Skip to content

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.

bash
export OPENAI_API_KEY="your_api_key_here"
cmd
setx OPENAI_API_KEY "your_api_key_here"
powershell
$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:

bash
npm install openai

After installing the OpenAI SDK, create a file named example.mjs and copy the example code into it:

bash
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:

bash
pip install openai

After installing the OpenAI SDK, create a file named example.py and copy the example code into it:

python
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:

bash
dotnet add package OpenAI

After installing the OpenAI SDK, create a file named example.cs and copy the example code into it:

csharp
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:

bash
<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:

java
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

bash
go get github.com/openai/openai-go

Example code(Chat Completion API request):

go
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

This documentation is licensed under CC BY-SA 4.0.