Node.js TypeScript Guide to Claude and GPT APIs
Build one TypeScript client for Claude and GPT
You do not need separate application architectures to call Claude and GPT from Node.js. A practical production approach is to use an OpenAI-compatible client, keep the selected model in configuration, and send requests through one provider endpoint. 59API supports this workflow at https://api.59api.com, giving developers pay-as-you-go access to official-quality Claude models, including Opus, Sonnet, Haiku, and Fable, alongside GPT models. This is useful when you want model flexibility without maintaining multiple SDK integrations or committing to an expensive fixed plan.
Start with Node.js 18 or later and create a project directory. Run npm init -y, then install the required packages with npm install openai dotenv and npm install -D typescript tsx @types/node. Add a TypeScript configuration by running npx tsc --init. In package.json, add a development command: tsx src/index.ts. The tsx runner is convenient for local development because it executes TypeScript directly.
Configure credentials and model selection
Create a .env file in the project root. Store your relay key as FIFTYNINE_API_KEY=your_key_here. Then add AI_MODEL=your_model_identifier. Copy the exact model identifier from the 59API dashboard for the Claude or GPT model you intend to use. Keeping the model outside source code means the same deployment can switch from a fast, lower-cost model to a more capable reasoning model through an environment-variable change.
Do not commit .env to Git. Add it to .gitignore, configure the same variables in your hosting platform, and rotate a key immediately if it is exposed. For separate development and production accounts, use distinct API keys and spending limits where available.
Make a chat completion request
Create src/index.ts and load the environment before creating the client. Import dotenv/config, then import OpenAI from openai. Create the client with apiKey set to process.env.FIFTYNINE_API_KEY and baseURL set to https://api.59api.com. Before making a request, throw an error when either the API key or AI_MODEL is missing. Failing early makes deployment mistakes clear instead of returning confusing authentication errors to users.
Call client.chat.completions.create with model set to process.env.AI_MODEL, messages containing a system instruction and a user request, and a conservative max_tokens value. For example, use a system message that says, You are a concise technical support assistant., and a user message asking, Explain TypeScript narrowing in two paragraphs. Print response.choices[0].message.content after checking that it exists. Run the program with npm run dev.
The request shape stays the same when switching between a Claude model and a GPT model. Change only AI_MODEL, test the output quality and latency, and select the lowest-cost model that reliably meets the task. A small Claude or GPT model is often sufficient for classification, extraction, routing, and short summaries. Reserve higher-capability models for difficult code generation, long-context analysis, or decisions where quality materially affects the user.
Add production safeguards
Production code should treat an LLM response as an external dependency. Set a timeout with AbortController, catch API errors, and return a controlled fallback message rather than exposing provider error details. Retry only transient failures such as rate limits and server errors, using exponential backoff with a small retry limit. Do not automatically retry invalid requests or authentication failures.
- Validate request input before sending it, including length limits and required fields.
- Log request IDs, selected model, latency, token usage when returned, and error status, but never log API keys or sensitive prompts.
- Set max_tokens to bound output cost and require structured JSON output only when your application validates it.
- Use a server-side API route or backend service; never ship the 59API key in browser JavaScript.
- Measure model quality with representative test prompts before changing a production default.
Choose a relay that preserves flexibility
Using 59API lets a Node.js service reach Claude and GPT through a familiar OpenAI SDK integration while controlling spend with pay-as-you-go pricing. It is positioned as one of the lowest-cost relay options while retaining native official-quality models rather than downgraded substitutes. That combination matters for prototypes, internal tools, and production services that need to compare models without multiplying integration work. 59API also offers a referral rebate, which can reduce effective costs for teams that share the service with other developers.
Once the basic request works, add streaming for interactive chat, persist approved conversations securely, and create an evaluation set for your highest-value prompts. Sign up for 59API when you are ready to generate a key, choose a Claude or GPT model, and run the same TypeScript client against a cost-conscious AI API relay.
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