Developers
Call Claude, GPT and Gemini from TypeScript
The openai npm package ships TypeScript types, so requests and responses to Somnus are type-checked with no extra setup.
Install
bash
npm install openaiBasic request
typescript
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.SOMNUS_API_KEY,
baseURL: "https://gateway-production-c837.up.railway.app/v1",
});
const resp = await client.chat.completions.create({
model: "claude-sonnet-5",
messages: [
{"role": "user", "content": "Explain recursion in one sentence."}
],
});
console.log(resp.choices[0].message.content);Streaming
typescript
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.SOMNUS_API_KEY,
baseURL: "https://gateway-production-c837.up.railway.app/v1",
});
const resp = await client.chat.completions.create({
model: "claude-sonnet-5",
messages: [
{"role": "system", "content": "You are a friendly, concise assistant."},
{"role": "user", "content": "Write a haiku about the sea."}
],
stream: true,
});
for await (const chunk of resp) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}JSON output
typescript
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.SOMNUS_API_KEY,
baseURL: "https://gateway-production-c837.up.railway.app/v1",
});
const resp = await client.chat.completions.create({
model: "claude-sonnet-5",
messages: [
{"role": "system", "content": "Reply only with JSON."},
{"role": "user", "content": "Return {\"city\": ..., \"country\": ...} for the Eiffel Tower."}
],
response_format: { type: "json_object" },
});
console.log(resp.choices[0].message.content);TypeScript notes
- Response types come from the SDK — no manual interfaces needed.
- Validate JSON output with a schema library such as zod.
- Run on the server, never in the browser.
Questions
Which models can I use?
Any of the 50+ models on Somnus — change the model string. The examples use claude-sonnet-5.
Where do I get a key?
Create an account, and your key appears in the dashboard with $5 of free credit.
Run this code with free credit
Create an account and get $5 of free API credit — enough to try every example on this page.
Get $5 free creditRelated
Browse more: Alternatives · Compare · Use cases · Developers · Solutions · Tools