OpenAI SDK Integration
The OpenAI SDK is the reference integration path used in the Quickstart, but the same pattern applies to any provider the gateway supports - only the URL segment and headers change. See Other Providers for the full list of supported providers and their URL segments.
Copy-paste examples for other languages: Python · PHP · Node.js · Go.
Configuration
Two things change from a direct OpenAI integration:
baseURLpoints at your Observra Gateway instead ofhttps://api.openai.com/v1, with/openaias the provider segment.- A Gateway Key is added, via a header, alongside your unchanged OpenAI API key.
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://gateway.observra.in/openai/v1",
apiKey: process.env.OPENAI_API_KEY, // unchanged - your own OpenAI key
defaultHeaders: {
"X-Gateway-Key": process.env.OBSERVRA_GATEWAY_KEY,
},
});
const response = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Summarize this in one sentence." }],
});
Everything else - streaming, tool calls, function calling, structured outputs - works exactly as it does calling OpenAI directly, because the gateway is a transparent proxy when the client and upstream protocols match (see Routing & Providers).
Alternative ways to pass the Gateway Key
If your environment can't set a custom header, the gateway also accepts:
Authorization: Bearer <gateway-key>instead ofapiKey+X-Gateway-Key(note: this is a different bearer than your OpenAI key - the OpenAI SDK's ownAuthorizationheader carries your OpenAI key, so useX-Gateway-Keyin the common case to avoid collision).- A compound bearer:
Authorization: Bearer <gateway-key>||<provider-key>if you want to pass both credentials in one place. - A body fallback field (
gateway_key) for clients that can't set headers at all.
See Gateway Authentication for the full precedence order.
Streaming
Set stream: true as usual:
const stream = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Count to five." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
The gateway relays SSE chunks as they arrive and extracts token/usage metrics from the same stream without altering what your client receives.
What the gateway does not change
- Your OpenAI API key is forwarded upstream on every request; Observra never stores or logs it.
- Response bodies are not persisted by default - only metadata (tokens, cost, latency, status). Full payload capture is opt-in per Environment.
- Error responses from OpenAI pass through unchanged; the gateway also records the failure as an observation with
status: "failed"and the error message.
Trace correlation
If you're already instrumented with OpenTelemetry, sending a traceparent header on your OpenAI calls lets you correlate the resulting observation with your broader trace - see Trace Context.