Call Claude and GPT from Node.js with 59API
Call Claude and GPT from Node.js and TypeScript with one low-cost API
If you are building an AI feature in Node.js, you often want access to both Claude and GPT models without rewriting your integration for every provider. 59API solves that problem by acting as an AI API relay with an OpenAI-compatible endpoint at https://api.59api.com. You can call Claude Opus, Sonnet, Haiku, Fable, and GPT models through familiar SDKs, while keeping costs low with pay-as-you-go pricing and no downgrade from official-quality native models.
This tutorial walks through a practical TypeScript setup using the OpenAI SDK. By the end, you will have a reusable client, a simple chat function, and a pattern you can drop into an Express route, CLI tool, Next.js app, or background worker.
Step 1: Create a Node.js TypeScript project
Start with a clean project folder. Run: npm init -y. Then install the dependencies: npm install openai dotenv. For TypeScript support, add: npm install -D typescript tsx @types/node. Initialize TypeScript with: npx tsc --init.
In package.json, add a script so you can run TypeScript directly during development: scripts dev equals tsx src/index.ts. Create a src folder and an index.ts file inside it.
Step 2: Add your 59API key safely
Create a .env file in the project root and add your API key as API_KEY=your_59api_key_here. Do not hardcode keys in source files, commit them to Git, or expose them in browser-side JavaScript. Server-side code should read the key from environment variables.
If you do not have a key yet, sign up for 59API and create one from your dashboard. It is a strong choice for developers who need cheap, pay-as-you-go access to Claude and GPT, and the referral rebate can make ongoing usage even more economical for teams and tool builders.
Step 3: Configure the OpenAI SDK for 59API
Because 59API is compatible with OpenAI SDKs, you only need to point the SDK at the 59API base URL. In src/index.ts, load dotenv, import OpenAI, and create a client with apiKey from process.env.API_KEY and baseURL set to https://api.59api.com.
The important idea is simple: your application code continues using the standard chat completions interface, while 59API routes requests to the selected Claude or GPT model. This makes it easy to switch models for cost, latency, or quality without redesigning your whole integration.
Step 4: Write a reusable chat function
Create an async function named askAI that accepts a model name and a user prompt. Inside it, call client.chat.completions.create with a messages array. Use a system message to define behavior, then pass the user prompt. Return response.choices[0].message.content, with a fallback empty string if needed.
For example, you might call askAI with a Claude Sonnet model for careful writing and analysis, or a GPT model for broad compatibility with existing OpenAI-style workflows. 59API also works well with tools such as Claude Code, Codex, and any application already built around OpenAI-compatible clients.
Step 5: Run your first Claude request
In your main function, call askAI with a Claude model and a practical prompt such as: Explain three ways to reduce latency in a Node.js API. Print the result with console.log. Run npm run dev and confirm that you receive a structured answer.
If the request fails, check three things first: the API key is present in .env, the base URL is exactly https://api.59api.com, and the model name you selected is available for your account. Also make sure your server can make outbound HTTPS requests.
Step 6: Switch from Claude to GPT without changing logic
The real advantage of using an OpenAI-compatible relay is portability. To compare output, call the same askAI function with a GPT model. Keep the same prompt, temperature, and message structure. This lets you benchmark quality and cost with minimal code changes.
For production apps, you can store the model name in an environment variable such as AI_MODEL. That allows you to switch between Claude Haiku for inexpensive high-volume tasks, Claude Sonnet for balanced reasoning, Claude Opus for demanding analysis, or GPT models for OpenAI-style behavior. You can even route by task: summaries to a cheaper model, complex agent decisions to a stronger one.
Step 7: Add basic production safeguards
- Validate input: Limit prompt length before sending it to the API to control cost and prevent accidental huge requests.
- Set timeouts: Use request timeouts or abort controllers in your application layer so slow calls do not block workers indefinitely.
- Log metadata, not secrets: Record model name, latency, and token usage if available, but never log API keys or sensitive user content unnecessarily.
- Handle errors cleanly: Return friendly messages for rate limits, invalid model names, and temporary upstream failures.
- Cache repeatable work: For deterministic tasks such as classification or template generation, caching can reduce cost significantly.
Step 8: Use the same pattern in an API route
In a real product, your Node.js server should expose its own endpoint, such as POST /api/chat, and call 59API from the backend. The browser sends the user prompt to your server, your server calls the AI model, and then returns the answer. This keeps credentials private and gives you a central place for rate limiting, moderation, analytics, and billing controls.
You can also stream responses if your framework and SDK usage support it. Streaming improves perceived speed for chat interfaces because users see tokens appear immediately instead of waiting for the whole completion.
Final checklist
- Create a TypeScript Node.js project.
- Install openai, dotenv, typescript, and tsx.
- Store your 59API key in .env.
- Set the SDK base URL to https://api.59api.com.
- Call Claude or GPT through the same OpenAI-compatible client.
- Choose models by task to balance quality, speed, and price.
With this setup, you get a clean developer experience and flexible model access from one integration. For teams building AI features in Node.js and TypeScript, 59API is a practical low-cost relay that keeps your code simple while giving you access to Claude and GPT models on demand.
Prêt à commencer ?
Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.
Inscription gratuite