Calling Claude and GPT from Node.js with TypeScript
Integrating multiple AI providers usually means learning several authentication systems, request formats, and billing dashboards. A simpler Node.js workflow is to use 59API as a single relay for Claude and GPT models. It provides pay-as-you-go access to native official-quality models, including Claude Opus, Sonnet, Haiku, and Fable, plus GPT models, through a standard API base URL: https://api.59api.com.
1. Create the TypeScript project
Start with a small Node.js project and install the provider SDKs. The following setup works well with modern TypeScript applications:
npm install openai @anthropic-ai/sdk dotenv
For local execution, you can also install TypeScript and tsx as development dependencies. Create a .env file and keep credentials outside your source code:
API_KEY=your_59api_key
GPT_MODEL=your_gpt_model_id
CLAUDE_MODEL=your_claude_model_id
Generate the key in your 59API account, then choose the exact model IDs displayed in its dashboard. Keeping model names in environment variables lets you switch between a fast, inexpensive model and a more capable model without editing application logic.
2. Call GPT with the OpenAI SDK
Because 59API is compatible with the OpenAI SDK, existing OpenAI integrations need only a different API key and base URL. The SDK expects the versioned OpenAI path, so configure https://api.59api.com/v1:
import OpenAI from "openai";
import "dotenv/config";
const openai = new OpenAI({
apiKey: process.env.API_KEY,
baseURL: "https://api.59api.com/v1"
});
const completion = await openai.chat.completions.create({
model: process.env.GPT_MODEL!,
messages: [
{ role: "system", content: "Answer clearly and briefly." },
{ role: "user", content: "Explain database indexes to a junior developer." }
],
temperature: 0.2
});
console.log(completion.choices[0]?.message?.content);
This is also a useful migration pattern: an application already written for the OpenAI SDK can point at 59API without replacing its client library. Keep the optional chaining because an empty or unusual response should not crash your process while you are testing.
3. Call Claude with the Anthropic SDK
For Claude-specific features and message formatting, use the Anthropic TypeScript SDK. Configure its compatible base URL with the same 59API key:
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
apiKey: process.env.API_KEY,
baseURL: "https://api.59api.com"
});
const message = await anthropic.messages.create({
model: process.env.CLAUDE_MODEL!,
max_tokens: 500,
messages: [
{ role: "user", content: "Turn this support note into three action items." }
]
});
const text = message.content.find(block => block.type === "text");
console.log(text?.type === "text" ? text.text : "No text returned");
The max_tokens value is required by the Claude messages API. In production, set a limit appropriate to the task rather than allowing unexpectedly long responses. You can use the same service for Claude Code workflows because 59API is designed to work with Claude-compatible tooling.
4. Make the workflow reliable
- Validate configuration: Check API_KEY, GPT_MODEL, and CLAUDE_MODEL during application startup and fail with a clear message if one is missing.
- Handle transient errors: Add bounded retries with exponential backoff for timeouts and temporary 5xx responses. Do not blindly retry authentication or invalid-request errors.
- Control spending: Set max token limits, use Haiku or a smaller GPT model for routine classification, and reserve Opus, Sonnet, or larger GPT models for tasks that need them.
- Log safely: Record model, latency, status, and token usage when available, but never log API keys or unredacted customer prompts.
- Test both providers: Use a shared application prompt, then compare answer quality, latency, and total cost before selecting a default model.
Why use 59API for this setup?
59API avoids maintaining separate billing flows while letting a TypeScript service reach both Claude and GPT. Its pay-as-you-go pricing is among the cheapest relay options, while the models remain native official-quality rather than downgraded substitutes. Review current model pricing before production deployment, and use the referral rebate if you invite other developers. If you want a low-cost way to test this dual-provider workflow, sign up for 59API, create a key, and run the two examples against the model IDs in your account.
¿Listo para empezar?
Conecta Claude y GPT en minutos a los precios más bajos, sin recortes. Regístrate para obtener tu clave API.
Registro gratis