OpenAI SDK Setup
Overview
Set up your development environment to call the OpenAI API using the SDK in your preferred language. This page covers how to set up your local development environment to use the OpenAI API. You can use officially supported SDKs, community libraries, or your favorite HTTP client.
Create and Export API Key
Before you begin, create an API key through the token instructions for secure API access. Store the key in a secure location, such as your .zshrc file or another text file on your computer. After generating the API key, export it as an environment variable in your terminal.
export OPENAI_API_KEY="your_api_key_here"setx OPENAI_API_KEY "your_api_key_here"$env:OPENAI_API_KEY="your_api_key_here"Note: The OpenAI SDK automatically reads your API key from the system environment. You can also set the API key directly in your code, but this is not secure, especially when sharing code. It's recommended to use environment variables to protect your key.
Install Official SDK
JavaScript
To use the OpenAI API in server-side JavaScript environments (such as Node.js, Deno, or Bun), you can use the official TypeScript and JavaScript OpenAI SDK.
Install SDK
Install the OpenAI SDK using your preferred package manager. Here's an example command using npm:
npm install openaiAfter installing the OpenAI SDK, create a file named example.mjs and copy the example code into it:
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: "https://api.agentsflare.com/v1",
});
const response = await client.responses.create({
model: "gpt-4.1",
input: "Write a one-sentence bedtime story about a unicorn.",
});
console.log(response.output_text);Execute with node example.mjs (or equivalent command for Deno or Bun), and you'll see the API request output in a few seconds.
Python
To use the OpenAI API in Python, use the official OpenAI Python SDK.
Install SDK
Install the OpenAI SDK using pip:
pip install openaiAfter installing the OpenAI SDK, create a file named example.py and copy the example code into it:
import openai
openai.api_key = "your_api_key_here"
openai.api_base = "https://api.agentsflare.com/v1"
response = openai.Completion.create(
model="gpt-4.1",
prompt="Write a one-sentence bedtime story about a unicorn.",
max_tokens=100,
temperature=0.7
)
print(response.choices[0].text.strip())Execute python example.py, and you'll see the API request output in a few seconds.
.NET
To use the OpenAI API in .NET, use the official OpenAI .NET SDK.
Install SDK
Install the OpenAI SDK using NuGet:
dotnet add package OpenAIAfter installing the OpenAI SDK, create a file named example.cs and copy the example code into it:
using OpenAI;
using OpenAI.Api;
using OpenAI.Api.Models;
var client = new OpenAIClient("your_api_key_here", "https://api.agentsflare.com/v1");
var response = await client.Responses.CreateAsync(new ResponseCreateRequest
{
Model = "gpt-4.1",
Input = "Write a one-sentence bedtime story about a unicorn.",
});
Console.WriteLine(response.OutputText);Execute dotnet run example.cs, and you'll see the API request output in a few seconds.
JAVA
To use the OpenAI API in Java, use the official OpenAI Java SDK.
Install SDK
Install the OpenAI SDK using Maven:
<dependency>
<groupId>com.openai</groupId>
<artifactId>openai-java</artifactId>
<version>1.0.0</version>
</dependency>After installing the OpenAI SDK, create a file named example.java and copy the example code into it:
import com.openai.OpenAIClient;
import com.openai.api.models.ResponseCreateRequest;
public class Example {
public static void main(String[] args) {
OpenAIClient client = new OpenAIClient("your_api_key_here", "https://api.agentsflare.com/v1");
ResponseCreateRequest request = new ResponseCreateRequest();
request.setModel("gpt-4.1");
request.setInput("Write a one-sentence bedtime story about a unicorn.");
var response = client.responses().create(request);
System.out.println(response.getOutputText());
}
}Execute java Example, and you'll see the API request output in a few seconds.
Go
go get github.com/openai/openai-goExample code(Chat Completion API request):
package main
import (
"context"
"fmt"
"log"
"os"
openai "github.com/openai/openai-go"
)
func main() {
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
res, err := client.ChatCompletions.Create(context.Background(), openai.ChatCompletionCreateParams{
Model: openai.F("gpt-4.1"),
Messages: openai.ChatCompletionMessageParamsArray{
{
Role: openai.ChatCompletionMessageRoleUser,
Content: openai.F("Write a one-sentence bedtime story about a unicorn."),
},
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(res.Choices[0].Message.Content)
}Execute go run example.go, and you'll see the API request output in a few seconds.
Other OpenAI Repositories
- tiktoken - Token counting (https://github.com/openai/tiktoken)
- simple-evals - Simple evaluation library (https://github.com/openai/simple-evals)
- mle-bench - Library for evaluating machine learning engineer agents (https://github.com/openai/mle-bench)
- gym - Reinforcement learning environments (https://github.com/openai/gym)
- swarm - Educational orchestration repository (https://github.com/openai/swarm)