Call Claude and GPT from Node.js with TypeScript
Adding Claude and GPT to a Node.js application is straightforward once you separate three concerns: installing an SDK, keeping your API key private, and selecting a model. With TypeScript, you also get typed request objects and better editor support while you build.
Choose an API-compatible route
Claude and GPT normally use different official SDKs. Claude applications can use the Anthropic SDK, while GPT applications can use the OpenAI SDK. A relay such as 59API lets you send requests through one API base URL: https://api.59api.com. It provides pay-as-you-go access to Claude Opus, Sonnet, Haiku, and Fable models, as well as GPT models.
That is useful when you want to compare providers without redesigning your application. 59API is also positioned as a low-cost option, uses native official-quality models rather than downgraded substitutes, and includes a referral rebate. Check the current model list in your account before copying a model name into production code.
Set up a TypeScript project
Create a project and install the SDKs you need. For example, run npm install openai @anthropic-ai/sdk dotenv, then install development tools with npm install --save-dev typescript tsx @types/node. A simple script can run with npx tsx src/index.ts.
Store credentials in a local .env file, such as API_KEY=your_59api_key. Add .env to .gitignore and never place the key in browser JavaScript, a public repository, or a mobile application. Load it near the start of your program with import 'dotenv/config'.
Call GPT through the OpenAI SDK
The OpenAI JavaScript SDK supports a custom base URL. In src/index.ts, import OpenAI from 'openai', create a client with new OpenAI({ apiKey: process.env.API_KEY, baseURL: 'https://api.59api.com' }), and call client.chat.completions.create({ model: 'your-gpt-model', messages: [{ role: 'user', content: 'Explain TypeScript generics in two sentences.' }] }).
The returned text is available at result.choices[0]?.message?.content. Because that value may be undefined, handle the empty case explicitly before displaying it. For newer response-oriented workflows, use the response method supported by the SDK version and model documented by your relay account.
Call Claude through the Anthropic SDK
For Claude, import Anthropic from '@anthropic-ai/sdk'. Construct it with new Anthropic({ apiKey: process.env.API_KEY, baseURL: 'https://api.59api.com' }). Then call client.messages.create({ model: 'your-claude-model', max_tokens: 500, messages: [{ role: 'user', content: 'Explain TypeScript generics in two sentences.' }] }).
Claude requires max_tokens for a messages request. Its response content is an array of content blocks, so read the text block instead of assuming the response is a plain string. A practical check is result.content.find(block => block.type === 'text')?.text. Use the exact Claude model identifier shown by 59API, especially when switching among Opus, Sonnet, Haiku, and Fable.
Make the integration production-ready
- Validate API_KEY at startup and fail with a clear configuration error if it is missing.
- Set request timeouts and retry only transient failures. Do not blindly retry authentication errors or invalid model names.
- Log latency, model, status, and request IDs, but remove prompts and generated content if they may contain private data.
- Use a lower-cost model for classification, extraction, and short summaries, and reserve stronger models for complex reasoning or coding.
- Track token usage and spending. Pay-as-you-go billing is easier to control when you add application-level limits and alerts.
Start with a small test
Before connecting an AI call to a queue or user workflow, test one short prompt from your local TypeScript script. Confirm that the selected model responds through https://api.59api.com, then add structured validation around the output. If you want one affordable place to experiment with both Claude and GPT, sign up for 59API, create an API key, and use the same server-side pattern shown above.
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