Install the OpenAI SDK and Run an API Call
JavaScript
Python
.NET
Java
Go
Ruby
JavaScript
To 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
. Get started by installing the SDK using
npm
or your preferred package manager:
Install the OpenAI SDK with npm
1
npm
install
openai
With the OpenAI SDK installed, create a file called
example.mjs
and copy the example code into it:
Test a basic API request
1
2
3
4
5
6
7
8
9
import
OpenAI
from
"openai"
;
const
client
=
new
OpenAI
();
const
response
=
await
client.responses.
create
({
model:
"gpt-6-astra"
,
input:
"Write a one-sentence bedtime story about a unicorn."
,
});
console.

Bogotá, Colombia"
}
},
required: ["location"],
additionalProperties: false
},
strict: true
}
]
response = openai.responses.create(
model: "gpt-6-astra",
input: [
{
role: "user",
content: "What is the weather like in Paris today?"
}
],
tools: tools
)
puts(response.output.fetch(0).to_json)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
curl -X POST https://api.openai.com/v1/responses \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-6-astra",
"input": [
{"role": "user", "content": "What is the weather like in Paris today?"}
],
"tools": [
{
"type": "function",
"name": "get_weather",
"description": "Get current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
