Your first request with the OpenAI SDK
In this tutorial you build a small TypeScript script that sends a chat completion through PlainField. By the end, you will have a working client you can reuse for any of PlainField’s 100+ models.
You do not change any provider SDK code — you only change the baseURL and use
your PlainField API key. PlainField speaks the OpenAI Chat Completions format,
so the OpenAI SDK works unmodified.
Prerequisites
- Node.js 20+
- A PlainField API key (
pt_live_...). Create one in the dashboard under Keys → API Keys — see the Quickstart.
1. Create the project
mkdir plainfield-client && cd plainfield-client
npm init -y
npm install openai
npm pkg set type=module2. Set your API key
Export your PlainField key so the script can read it:
export PLAINFIELD_API_KEY=pt_live_...3. Write the script
Create index.mjs:
import OpenAI from 'openai'
const client = new OpenAI({
// Point the SDK at the PlainField gateway.
baseURL: 'https://api.plainfield-ai.com/ai',
// Your PlainField API key authenticates the request.
apiKey: process.env.PLAINFIELD_API_KEY
})
const response = await client.chat.completions.create({
model: 'openai/gpt-4o-mini',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Say hello in one sentence.' }
],
max_tokens: 100
})
console.log(response.choices[0].message.content)4. Run it
node index.mjsExpected output
Hello! It's great to meet you — how can I help today?The exact wording varies by model and run; the point is that you get a single line of assistant text back.
What just happened
- The OpenAI SDK sent a standard Chat Completions request to
https://api.plainfield-ai.com/ai/chat/completions. - PlainField read the
openai/prefix on themodelfield and proxied to OpenAI. - PlainField logged the request and estimated its cost from the response’s token usage. Open Analytics in the dashboard to see it.
Stream the response
To render tokens as they arrive instead of waiting for the full reply, set
stream: true and iterate over the chunks:
const stream = await client.chat.completions.create({
model: 'openai/gpt-4o-mini',
messages: [{ role: 'user', content: 'Stream a short poem about the sea.' }],
stream: true
})
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? '')
}Streaming works for every model that supports it — PlainField passes the chunks straight through.
Try another provider
Change one string. Switch the model to a different provider:
model: 'anthropic/claude-3-5-sonnet'See every supported model in Providers and models.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
401 from the gateway | Missing or invalid API key | Set PLAINFIELD_API_KEY to a valid pt_live_ key. See Authenticate with an API key. |
400 unknown model | Wrong provider/model string | Use a supported model. See Providers and models. |
Connection error | Wrong baseURL | Use https://api.plainfield-ai.com/ai. |
Next steps
- Attribute requests to prompts and users, then read their cost: Track prompt costs
- See the full endpoint surface: API reference