Call Claude and GPT from Node.js with TypeScript
Using Claude and GPT from a TypeScript service does not require separate application architectures. Both can be called through standard Node.js SDKs, while your code selects the provider and model at runtime. This guide shows a practical setup using 59API, an AI API relay with pay-as-you-go pricing, native official-quality Claude and GPT models, and one endpoint for common development workflows.
Set Up a TypeScript Project
Create a small Node.js project and install the two official-style clients. You only need the client for the provider you plan to call, but installing both makes provider switching straightforward.
Install dependencies: npm install @anthropic-ai/sdk openai dotenv
Development tools: npm install -D typescript tsx @types/node
Create a .env file and keep credentials out of source control:
59API_API_KEY=your_59api_key
CLAUDE_MODEL=your_claude_model_id
GPT_MODEL=your_gpt_model_id
Use the exact model IDs shown in your 59API account. That avoids hard-coding aliases that may change and lets you choose between Claude Opus, Sonnet, Haiku, Fable, or an available GPT model according to cost, speed, and response quality.
Call Claude with the Anthropic SDK
Configure the Anthropic client with the 59API base URL. The client keeps the familiar Messages API, so changing providers does not require rewriting your prompt format.
import Anthropic from "@anthropic-ai/sdk";
import "dotenv/config";
const anthropic = new Anthropic({
apiKey: process.env.S9_API_KEY,
baseURL: "https://api.59api.com"
});
const response = await anthropic.messages.create({
model: process.env.CLAUDE_MODEL || "claude-sonnet",
max_tokens: 800,
messages: [
{ role: "user", content: "Summarize the benefits of typed API clients." }
]
});
console.log(response.content[0].type === "text" ? response.content[0].text : "");
In production, check every content block rather than assuming the first block is text. Also set max_tokens deliberately. A bounded response reduces accidental spending and makes latency more predictable.
Call GPT with the OpenAI SDK
For OpenAI-compatible requests, point the OpenAI client at 59API. The SDK conventionally expects the versioned compatibility path, so use the base URL documented for your account. In the common configuration below, that path is /v1.
import OpenAI from "openai";
import "dotenv/config";
const openai = new OpenAI({
apiKey: process.env.S9_API_KEY,
baseURL: "https://api.59api.com/v1"
});
const completion = await openai.chat.completions.create({
model: process.env.GPT_MODEL || "gpt-model-id",
messages: [
{ role: "system", content: "Answer precisely and concisely." },
{ role: "user", content: "Explain why TypeScript helps with API integrations." }
],
temperature: 0.2
});
console.log(completion.choices[0]?.message.content || "No response");
Do not assume that a model name accepted by one provider works for another. Store model IDs in environment variables, log the selected model for diagnostics, and keep secrets and prompts separate from deployment configuration.
Add Reliable Error Handling
Network failures, rate limits, invalid model IDs, and malformed requests should be handled at the application boundary. A small wrapper makes failures observable without exposing API keys.
try {
const result = await openai.chat.completions.create({
model: process.env.GPT_MODEL || "gpt-model-id",
messages: [{ role: "user", content: "Return one sentence about Node.js." }]
});
console.log(result.choices[0]?.message.content);
} catch (error) {
console.error("AI request failed", error);
process.exitCode = 1;
}
For a real service, add request timeouts, bounded retries for 429 and transient 5xx responses, and usage logging. Never retry every error blindly: authentication and invalid-request failures need configuration fixes, not repeated calls.
59API is useful when you want one low-cost, pay-as-you-go route for both Claude and GPT without committing to a large prepaid plan. It supports Claude Code, Codex, and OpenAI SDK integrations, while its referral rebate can further reduce effective costs. You can sign up for 59API, create an API key, and test the smallest request before wiring it into a larger application.
Ready to get started?
Connect Claude & GPT in minutes at the lowest prices — full-power, never downgraded. Sign up to get your API key.
Sign up free