Repository Public
1# SDKs and CLI23> For the complete documentation index, see [llms.txt](/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.45This page covers the main ways to build with the [OpenAI API](https://developers.openai.com/api/reference/overview): official SDKs for application code, the OpenAI CLI for shell-native workflows, the Agents SDK for orchestration, or your own preferred HTTP client.67## Create and export an API key89Before you begin, [create an API key in the dashboard](https://platform.openai.com/api-keys), which you'll use to securely [access the API](https://developers.openai.com/api/reference/overview). Store the key in a safe location, like a [`.zshrc` file](https://www.freecodecamp.org/news/how-do-zsh-configuration-files-work/) or another text file on your computer. Once you've generated an API key, export it as an [environment variable](https://en.wikipedia.org/wiki/Environment_variable) in your terminal.10111213macOS / Linux1415 Export an environment variable on macOS or Linux systems1617```bash18export OPENAI_API_KEY="your_api_key_here"19```2021 2223 2425 26Windows2728 Export an environment variable in PowerShell2930```bash31setx OPENAI_API_KEY "your_api_key_here"32```33343536OpenAI SDKs are configured to automatically read your API key from the system environment.3738## Install an official SDK39404142JavaScript4344 4546To use the OpenAI API in server-side JavaScript environments like Node.js, Deno, or Bun, you can use the official [OpenAI SDK for TypeScript and JavaScript](https://github.com/openai/openai-node). Get started by installing the SDK using [npm](https://www.npmjs.com/) or your preferred package manager:4748Install the OpenAI SDK with npm4950```bash51npm install openai52```535455With the OpenAI SDK installed, create a file called `example.mjs` and copy the example code into it:5657Test a basic API request5859```javascript60import OpenAI from "openai";61const client = new OpenAI();6263const response = await client.responses.create({64 model: "gpt-6-astra",65 input: "Write a one-sentence bedtime story about a unicorn.",66});6768console.log(response.output_text);69```707172Execute the code with `node example.mjs` (or the equivalent command for Deno or Bun). In a few moments, you should see the output of your API request.7374[Learn more on GitHub75767778 Discover more SDK capabilities and options on the library's GitHub README.](https://github.com/openai/openai-node)798081 8283 8485 86Python8788 8990To use the OpenAI API in Python, you can use the official [OpenAI SDK for Python](https://github.com/openai/openai-python). Get started by installing the SDK using [pip](https://pypi.org/project/pip/):9192Install the OpenAI SDK with pip9394```bash95pip install openai96```979899With the OpenAI SDK installed, create a file called `example.py` and copy the example code into it:100101Test a basic API request102103```python104from openai import OpenAI105106client = OpenAI()107108response = client.responses.create(109 model="gpt-6-astra",110 input="Write a one-sentence bedtime story about a unicorn.",111)112113print(response.output_text)114```115116117Execute the code with `python example.py`. In a few moments, you should see the output of your API request.118119[Learn more on GitHub120121122123 Discover more SDK capabilities and options on the library's GitHub README.](https://github.com/openai/openai-python)124125126 127128 129130 131.NET132133 134135In collaboration with Microsoft, OpenAI provides an officially supported API client for C#. You can install it with the .NET CLI from [NuGet](https://www.nuget.org/).136137```138dotnet add package OpenAI139```140141A simple API request to the [Responses API](https://developers.openai.com/api/reference/resources/responses) would look like this:142143Test a basic API request144145```csharp146using OpenAI.Responses;147#pragma warning disable OPENAI001148149string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;150ResponsesClient client = new(key);151152ResponseResult response = await client.CreateResponseAsync(153 "gpt-6-astra",154 "Say 'this is a test.'"155);156157Console.WriteLine($"[ASSISTANT]: {response.GetOutputText()}");158```159160161 162163 164165 166Java167168 169170OpenAI provides an API helper for the Java programming language, currently in beta. You can include the Maven dependency using the following configuration:171172```xml173<dependency>174 <groupId>com.openai</groupId>175 <artifactId>openai-java</artifactId>176 <version>4.65.0</version>177</dependency>178```179180181A simple API request to [Responses API](https://developers.openai.com/api/reference/resources/responses) would look like this:182183Test a basic API request184185```java186import com.openai.client.OpenAIClient;187import com.openai.client.okhttp.OpenAIOkHttpClient;188import com.openai.models.responses.Response;189import com.openai.models.responses.ResponseCreateParams;190191public class Main {192 public static void main(String[] args) {193 OpenAIClient client = OpenAIOkHttpClient.fromEnv();194195 ResponseCreateParams params =196 ResponseCreateParams.builder().input("Say this is a test").model("gpt-6-astra").build();197198 Response response = client.responses().create(params);199 response.output().stream()200 .flatMap(item -> item.message().stream())201 .flatMap(message -> message.content().stream())202 .flatMap(content -> content.outputText().stream())203 .forEach(outputText -> System.out.println(outputText.text()));204 }205}206```207208209To learn more about using the OpenAI API in Java, check out the GitHub repo linked below!210211[Learn more on GitHub212213214215 Discover more SDK capabilities and options on the library's GitHub README.](https://github.com/openai/openai-java)216217218 219220 221222 223Go224225 226227OpenAI provides an API helper for the Go programming language, currently in beta. You can import the library using the code below:228229```go230import (231 "github.com/openai/openai-go/v3" // imported as openai232)233```234235236A first API request to the [Responses API](https://developers.openai.com/api/reference/resources/responses) would look like this:237238Test a basic API request239240```go241package main242243import (244 "context"245 "fmt"246247 "github.com/openai/openai-go/v3"248 "github.com/openai/openai-go/v3/responses"249)250251func main() {252 client := openai.NewClient()253254 resp, err := client.Responses.New(context.TODO(), responses.ResponseNewParams{255 Model: "gpt-6-astra",256 Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Say this is a test")},257 })258 if err != nil {259 panic(err.Error())260 }261262 fmt.Println(resp.OutputText())263}264```265266267To learn more about using the OpenAI API in Go, check out the GitHub repo linked below!268269[Learn more on GitHub270271272273 Discover more SDK capabilities and options on the library's GitHub README.](https://github.com/openai/openai-go)274275276 277278 279280 281Ruby282283 284285To use the OpenAI API in Ruby, you can use the official [OpenAI SDK for Ruby](https://github.com/openai/openai-ruby). Get started by adding the gem to your application:286287Install the OpenAI SDK with Bundler288289```ruby290gem "openai"291```292293294With the OpenAI SDK installed, create a file called `example.rb` and copy the example code into it:295296Test a basic API request297298```ruby299require "openai"300301openai = OpenAI::Client.new302303response = openai.responses.create(304 model: "gpt-6-astra",305 input: "Write a one-sentence bedtime story about a unicorn."306)307308puts(response.output_text)309```310311312Execute the code with `ruby example.rb`. In a few moments, you should see the output of your API request.313314[Learn more on GitHub315316317318 Discover more SDK capabilities and options on the library's GitHub README.](https://github.com/openai/openai-ruby)319320321 322323 324325 326CLI327328 329330To call the OpenAI API directly from your terminal, install the generated `openai` command-line tool:331332Install the OpenAI CLI with Homebrew333334```bash335brew install openai/tools/openai336```337338339Then run a basic API request from your shell:340341Test a basic API request342343```bash344openai responses create \345 --model "gpt-6-astra" \346 --input "Write a one-sentence bedtime story about a unicorn." \347 --raw-output \348 --transform 'output.#(type=="message").content.0.text'349```350351352Use the CLI for repeatable terminal workflows such as extracting structured data from files, generating images, creating speech, and composing API calls with shell tools like `jq`.353354[OpenAI CLI guide355356357358 Learn more about CLI workflows and command patterns.](https://developers.openai.com/api/docs/libraries/openai-cli)359360361362## Use the Agents SDK363364Use the official OpenAI SDKs above for direct API requests. Use the Agents SDK365when your application needs code-first orchestration for agents, tools,366handoffs, guardrails, tracing, or sandbox execution.367368If you are deciding between direct API requests and code-first orchestration,369see [how the Responses API compares with the Agents SDK](https://developers.openai.com/api/docs/guides/agents#agents-sdk-vs-responses-api).370371[Agents SDK quickstart372373374375 Build your first agent with the Agents SDK.](https://developers.openai.com/api/docs/guides/agents/quickstart)376377- [OpenAI Agents SDK for TypeScript](https://github.com/openai/openai-agents-js)378- [OpenAI Agents SDK for Python](https://github.com/openai/openai-agents-python)379380## Azure OpenAI libraries381382Microsoft's Azure team maintains libraries that are compatible with both the OpenAI API and Azure OpenAI services. Read the library documentation below to learn how you can use them with the OpenAI API.383384- [Azure OpenAI client library for .NET](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/openai/Azure.AI.OpenAI)385- [Azure OpenAI client library for JavaScript](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai)386- [Azure OpenAI client library for Java](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/openai/azure-ai-openai)387- [Azure OpenAI client library for Go](https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/ai/azopenai)388389---390391## Community libraries392393The libraries below are built and maintained by the broader developer community. You can also [watch our OpenAPI specification](https://github.com/openai/openai-openapi) repository on GitHub to get timely updates on when we make changes to our API.394395Please note that OpenAI does not verify the correctness or security of these projects. **Use them at your own risk!**396397### Clojure398399- [openai-clojure](https://github.com/wkok/openai-clojure) by [wkok](https://github.com/wkok)400401### Dart/Flutter402403- [openai](https://github.com/anasfik/openai) by [anasfik](https://github.com/anasfik)404405### Delphi406407- [DelphiOpenAI](https://github.com/HemulGM/DelphiOpenAI) by [HemulGM](https://github.com/HemulGM)408409### Elixir410411- [openai.ex](https://github.com/mgallo/openai.ex) by [mgallo](https://github.com/mgallo)412413### Kotlin414415- [openai-kotlin](https://github.com/Aallam/openai-kotlin) by [Mouaad Aallam](https://github.com/Aallam)416417### PHP418419- [orhanerday/open-ai](https://packagist.org/packages/orhanerday/open-ai) by [orhanerday](https://github.com/orhanerday)420- [openai-php client](https://github.com/openai-php/client) by [openai-php](https://github.com/openai-php)421422### Rust423424- [async-openai](https://github.com/64bit/async-openai) by [64bit](https://github.com/64bit)425426### Scala427428- [openai-scala-client](https://github.com/cequence-io/openai-scala-client) by [cequence-io](https://github.com/cequence-io)429430### Swift431432- [AIProxySwift](https://github.com/lzell/AIProxySwift) by [Lou Zell](https://github.com/lzell)433- [OpenAIKit](https://github.com/dylanshine/openai-kit) by [dylanshine](https://github.com/dylanshine)434- [OpenAI](https://github.com/MacPaw/OpenAI/) by [MacPaw](https://github.com/MacPaw)435436### Unity437438- [com.openai.unity](https://github.com/RageAgainstThePixel/com.openai.unity) by [RageAgainstThePixel](https://github.com/RageAgainstThePixel)439440### Unreal Engine441442- [OpenAI-Api-Unreal](https://github.com/KellanM/OpenAI-Api-Unreal) by [KellanM](https://github.com/KellanM)443444## Other OpenAI repositories445446- [tiktoken](https://github.com/openai/tiktoken) - counting tokens447- [simple-evals](https://github.com/openai/simple-evals) - simple evaluation library448- [mle-bench](https://github.com/openai/mle-bench) - library to evaluate machine learning engineer agents449- [gym](https://github.com/openai/gym) - reinforcement learning library450- [swarm](https://github.com/openai/swarm) - educational orchestration repository