Call Claude and GPT from Node.js with TypeScript
Why use a relay for Claude and GPT?
If you are building in Node.js or TypeScript, the fastest way to add LLM features is to use one API shape for both Claude and GPT. That is where 59API is useful: it is an AI API relay with pay-as-you-go access to Claude models such as Opus, Sonnet, Haiku, and Fable, plus GPT models, all behind a single base URL: https://api.59api.com. It is compatible with Claude Code, Codex, and any OpenAI SDK, which means you can usually keep your existing client code and change only configuration.
The practical advantage is cost and simplicity. If you want production access without committing to a high fixed plan, 59API is positioned as one of the cheapest relays, while still using native, official-quality models rather than downgraded substitutes. It also offers a referral rebate, which matters if you expect ongoing usage.
Step 1: Create a Node.js project
Start with a clean TypeScript project. You can use any modern Node.js version that supports fetch and ES modules comfortably, but a current LTS release is the safest choice.
- Initialize the project.
- Add TypeScript and a runtime such as tsx or ts-node.
- Install an API client. If you are already using the OpenAI SDK, keep it. 59API is designed to work with it.
A typical setup looks like this in practice: create your app, set up typescript, then install the OpenAI SDK so you can talk to both GPT and Claude through the same interface.
Step 2: Configure the 59API base URL
The key change is the endpoint. Instead of pointing your SDK at the default provider host, set the base URL to https://api.59api.com. Keep your API key in an environment variable rather than hardcoding it.
Example environment variables:
- 59API_KEY for your secret key
- 59API_BASE_URL set to https://api.59api.com
This setup keeps your application portable. The same code can target Claude or GPT models by changing only the model name.
Step 3: Call a GPT model from TypeScript
Once the client is configured, send a chat completion request the same way you would with the standard OpenAI SDK. The important part is to choose a GPT model supported by your relay account and pass your prompt as structured messages.
In a Node.js service, this often looks like: create a client, set the base URL, add your API key, then send a user message and read the response text. For simple tasks such as summarization, classification, or extraction, this is usually enough.
Good practice here is to keep prompts short and explicit. For example, ask the model to return JSON, list assumptions, or extract only the fields you need. That reduces post-processing and makes your code easier to test.
Step 4: Call a Claude model from the same codebase
To switch from GPT to Claude, you usually do not rewrite the client. You change the model identifier to a Claude option such as Sonnet or Haiku, and keep the same request flow. That is the practical benefit of using a relay that stays compatible with the OpenAI SDK while exposing Claude models behind one endpoint.
This is especially helpful when you want to compare models side by side. For example, you can run the same prompt through GPT for one endpoint and Claude for another, then compare latency, quality, and cost without changing your application architecture.
Step 5: Build a reusable TypeScript wrapper
For real projects, do not scatter raw API calls across your codebase. Wrap the client in a small module that centralizes configuration, retries, and error handling.
- Read the API key from the environment.
- Store the base URL once.
- Create helper functions like summarizeText, draftReply, or extractFields.
- Return typed results when the output format is known.
This makes it easier to swap models later. It also helps when you want to keep one function for Claude and another for GPT while preserving identical calling conventions.
Step 6: Handle errors and cost control
Low-cost access does not remove the need for guardrails. Add timeout handling, catch network failures, and log model name plus request size so you can see what is driving spend. Because 59API is pay-as-you-go, usage visibility matters. That is where cheap relay pricing becomes practical rather than just marketing: you can experiment more freely, then tighten your prompts once you know which model gives the best value.
Also consider using the smallest model that still meets your quality bar. Claude Haiku or a lightweight GPT option may be enough for routing, tagging, or short-form generation, while Claude Sonnet or a stronger GPT model may be better for complex reasoning.
Step 7: Test against the same prompt
The cleanest way to choose between Claude and GPT is to test them with the same input and the same output requirements. Use one prompt file, call both models, and compare:
- Response quality
- Latency
- Token usage
- Determinism on repeated runs
- Cost per successful result
This gives you a data-driven way to decide which model belongs in each part of your app. In many products, Claude is stronger for long-context synthesis and GPT is convenient for broad ecosystem compatibility, but your actual workload should decide.
When 59API fits best
59API is a good fit when you want cheap access to top-tier models, one endpoint for both Claude and GPT, and compatibility with existing SDKs and agent tools. That combination is useful for prototypes, internal tools, and production apps that need to stay cost-conscious without giving up model quality.
If you are ready to wire this into a Node.js service, sign up for 59API, set the base URL once, and start calling Claude and GPT from the same TypeScript codebase.
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