0% found this document useful (0 votes)
47 views13 pages

OpenAI SDK Developer Guide Quickstart JS Python v1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views13 pages

OpenAI SDK Developer Guide Quickstart JS Python v1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

OpenAI SDK Developer Guide

Quickstart for Node.js & Python


Author: MPS Prepared with GPT-5 Thinking
License: Creative Commons Attribution 4.0

vendor neutral quickstart for the official OpenAI SDKs: install, initialize, request patterns, structured output, tool calling
OpenAI SDK Developer Guide Quickstart for Node.js & Python v1.0 2025-09-02 License: CC BY 4.0

Table of Contents
Table of Contents
1. 1. Install & Configure
2. 2. Initialize the Client (Node.js)
3. 3. Initialize the Client (Python)
4. 4. Text Generation (Responses-like API)
5. 5. JSON / Structured Output
6. 6. Tool Calling (Function Calling)
7. 7. Vision (Images as Input)
8. 8. File Usage & Retrieval
9. 9. Streaming (Conceptual)
10. 10. Realtime (Conceptual)
11. 11. Safety & Security
OpenAI SDK Developer Guide Quickstart for Node.js & Python v1.0 2025-09-02 License: CC BY 4.0

1. 1. Install & Configure


1. Install & Configure
Create a new project and install the official SDKs.
Node.js: npm install openai
Python: pip install openai
Set your API key as an environment variable: OPENAI_API_KEY. Never hardcode secrets; use .env or
your secret manager.
OpenAI SDK Developer Guide Quickstart for Node.js & Python v1.0 2025-09-02 License: CC BY 4.0

2. 2. Initialize the Client (Node.js)


2. Initialize the Client (Node.js)
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
OpenAI SDK Developer Guide Quickstart for Node.js & Python v1.0 2025-09-02 License: CC BY 4.0

3. 3. Initialize the Client (Python)


3. Initialize the Client (Python)
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY from env
OpenAI SDK Developer Guide Quickstart for Node.js & Python v1.0 2025-09-02 License: CC BY 4.0

4. 4. Text Generation (Responses-like API)


4. Text Generation (Responses-like API)
Basic single-turn request in JS and Python. Replace the model name with one you have access to.
// Node.js
const res = await client.responses.create({
model: "gpt-4o-mini",
input: "Give me three bullet points about data governance."
});
console.log(res.output_text);
# Python
resp = client.responses.create(
model="gpt-4o-mini",
input="Give me three bullet points about data governance."
)
print(resp.output_text)
OpenAI SDK Developer Guide Quickstart for Node.js & Python v1.0 2025-09-02 License: CC BY 4.0

5. 5. JSON / Structured Output


5. JSON / Structured Output
Ask the model to return a typed object. Validate before using.
// Node.js
const res = await client.responses.create({
model: "gpt-4o-mini",
input: "Return a JSON object with title and bullets about SKU standards.",
response_format: { type: "json_schema", json_schema: {
name: "Notes",
schema: { type: "object", properties: {
title: { type: "string" },
bullets: { type: "array", items: { type: "string" } }
}, required: ["title","bullets"] }
}}
});
const data = JSON.parse(res.output[0].content[0].text);
OpenAI SDK Developer Guide Quickstart for Node.js & Python v1.0 2025-09-02 License: CC BY 4.0

6. 6. Tool Calling (Function Calling)


6. Tool Calling (Function Calling)
Define tools the model may call. Execute matched functions on your server and return results
back.
// Node.js
const tools = [{
type: "function",
function: {
name: "get_product_price",
description: "Lookup current price by SKU",
parameters: { type: "object", properties: { sku: { type: "string" } }, required: ["sku"]
}
}
}];

const res = await client.responses.create({


model: "gpt-4o-mini",
input: "What is the price of CO-F-FOUND-STICK-S2-30ML-SINGLE?",
tools,
tool_choice: "auto"
});

// Detect a function call in res and dispatch:


/* pseudo
OpenAI SDK Developer Guide Quickstart for Node.js & Python v1.0 2025-09-02 License: CC BY 4.0

7. 7. Vision (Images as Input)


7. Vision (Images as Input)
Send an image URL or base64 content; the model returns text.
// Node.js
const res = await client.responses.create({
model: "gpt-4o-mini",
input: [{
role: "user",
content: [
{ type: "input_text", text: "Extract any printed price from this label." },
{ type: "input_image", image_url: "https://example.com/label.jpg" }
]
}]
});
OpenAI SDK Developer Guide Quickstart for Node.js & Python v1.0 2025-09-02 License: CC BY 4.0

8. 8. File Usage & Retrieval


8. File Usage & Retrieval
Upload files for use with responses/assistants or for retrieval in your app. Keep size and type
within limits and apply retention policies.
OpenAI SDK Developer Guide Quickstart for Node.js & Python v1.0 2025-09-02 License: CC BY 4.0

9. 9. Streaming (Conceptual)
9. Streaming (Conceptual)
Use the SDK's streaming helpers or event APIs to render tokens or tool-calls as they arrive.
Always include timeouts, backoff, and a max token budget.
OpenAI SDK Developer Guide Quickstart for Node.js & Python v1.0 2025-09-02 License: CC BY 4.0

10. 10. Realtime (Conceptual)


10. Realtime (Conceptual)
For ultra-low latency voice or multimodal, use the Realtime API over WebSocket/WebRTC. It
supports input/output audio and server events.
OpenAI SDK Developer Guide Quickstart for Node.js & Python v1.0 2025-09-02 License: CC BY 4.0

11. 11. Safety & Security


11. Safety & Security
Follow least-privilege for API keys; perform input/output filtering; log reasons for tool
actions; store audit trails; respect user privacy and consent.

You might also like