Use OpenAI’s SDKs
Point OpenAI’s JavaScript or Python SDK at your project, and keep the rest of your code.
OpenAI’s SDKs add /chat/completions to a base URL, so the base URL carries your project: it is AI_URL, https://ai-api.digitospace.com/v1/projects/$PROJECT_ID. Change two settings, and the rest of your code stays as it is.
Before you begin
Set up your environment, so AI_API_KEY and AI_URL are set, and install the SDK: npm install openai or pip install openai.
Create a client
import OpenAI from "openai"
const client = new OpenAI({
apiKey: process.env.AI_API_KEY,
baseURL: process.env.AI_URL,
})Send a message
const answer = await client.chat.completions.create({
model: "openai/gpt-oss-120b",
messages: [
{ role: "system", content: "Answer in one short paragraph." },
{ role: "user", content: "Habari? Tell me about Zanzibar." },
],
max_tokens: 500,
})
console.log(answer.choices[0].message.content)What is different from OpenAI
- Models have our names, such as
openai/gpt-oss-120b. List them withclient.models.list(), or see Models. nmust be 1, or left out.- Fields we do not know are refused with Invalid request, so a typo never silently does nothing.
- Usage has one more field,
units: the AI units the call used. - Errors have OpenAI’s shape, with our codes. See Error codes.
Retries
The SDKs retry rate limits and failures on our side on their own, waiting as long as Retry-After says. A call refused because the project has used its month’s units is not retried: it would only be refused again.
Last updated