The official openai npm package works with Promptix unchanged, in Node.js 18+, Bun, Deno and edge runtimes. Point it at the Promptix base URL and use your Promptix key.
Install
npm install openaiConfigure the client
Set baseURL and apiKey. Create the client once and reuse it.
import OpenAI from 'openai'; export const promptix = new OpenAI({ baseURL: 'https://promptix.tn/api/v1', // the only line that changes apiKey: process.env.PROMPTIX_API_KEY,});Or, without changing code, set the environment variables the SDK reads by default:
export OPENAI_BASE_URL="https://promptix.tn/api/v1"export OPENAI_API_KEY="$PROMPTIX_API_KEY" # Existing code that calls new OpenAI() with no options now goes through PromptixServer only
Do not create the client in browser code and do not use dangerouslyAllowBrowser: your key would be visible to anyone. Call Promptix from a route handler, a server action or your API.
Chat completion
import { promptix } from './client.js'; const completion = await promptix.chat.completions.create({ model: 'anthropic/claude-sonnet-4', messages: [ { role: 'system', content: 'You are a helpful assistant who answers in French.' }, { role: 'user', content: 'Explain what a VAT number is in two sentences.' }, ], temperature: 0.3, max_tokens: 300,}); console.log(completion.choices[0].message.content);console.log(completion.usage?.total_tokens, 'tokens');Streaming
Pass stream: true and iterate with for await. See Streaming for the event format.
const stream = await promptix.chat.completions.create({ model: 'openai/gpt-4o-mini', messages: [{ role: 'user', content: 'List five dishes from Tunisian cuisine.' }], stream: true,}); for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content ?? '');}In a Next.js route handler, forward the tokens to the browser as they arrive:
// app/api/chat/route.ts: stream the answer to the browser as plain textimport OpenAI from 'openai'; const promptix = new OpenAI({ baseURL: 'https://promptix.tn/api/v1', apiKey: process.env.PROMPTIX_API_KEY,}); export async function POST(req: Request) { const { prompt } = await req.json(); const stream = await promptix.chat.completions.create({ model: 'openai/gpt-4o-mini', messages: [{ role: 'user', content: String(prompt) }], max_tokens: 800, stream: true, }); const encoder = new TextEncoder(); const body = new ReadableStream({ async start(controller) { try { for await (const chunk of stream) { const text = chunk.choices[0]?.delta?.content; if (text) controller.enqueue(encoder.encode(text)); } controller.close(); } catch (err) { controller.error(err); } }, }); return new Response(body, { headers: { 'Content-Type': 'text/plain; charset=utf-8' } });}Tool calling
const tools = [ { type: 'function', function: { name: 'get_order_status', description: 'Look up an order by its number', parameters: { type: 'object', properties: { orderId: { type: 'string' } }, required: ['orderId'], }, }, },]; const messages = [{ role: 'user', content: 'Where is my order 10452?' }];const first = await promptix.chat.completions.create({ model: 'openai/gpt-4o-mini', messages, tools });const call = first.choices[0].message.tool_calls?.[0]; if (call) { const args = JSON.parse(call.function.arguments); const result = { orderId: args.orderId, status: 'shipped', eta: '2 days' }; // your own lookup const second = await promptix.chat.completions.create({ model: 'openai/gpt-4o-mini', tools, messages: [ ...messages, first.choices[0].message, { role: 'tool', tool_call_id: call.id, content: JSON.stringify(result) }, ], }); console.log(second.choices[0].message.content);}Errors and retries
The SDK retries 408, 409, 429 and 5xx responses twice by default (maxRetries option) and throws OpenAI.APIError subclasses. See Errors and limits.
import OpenAI from 'openai'; try { await promptix.chat.completions.create({ model: 'openai/gpt-4o-mini', messages: [{ role: 'user', content: 'Hello' }], });} catch (err) { if (err instanceof OpenAI.APIError) { // err.status: 401, 402, 429...; err.code: 'key_monthly_limit' for the per-key limit console.error(err.status, err.code, err.message); } throw err;}What is not available
Only chat completions are exposed. embeddings, images, audio, responses, files and models.list() are not available through the Promptix base URL. Use the public model list to discover models.