59API

← Voltar aos guias

Calling Claude and GPT from Node.js: Pitfalls

Guias · EN · 2026-09-06

Why Claude and GPT integrations fail in practice

Calling a language model from Node.js looks simple: install an SDK, add an API key, and send a prompt. Most production problems come from configuration details, mismatched response formats, leaked credentials, or uncontrolled usage rather than from the model call itself. The following pitfalls apply whether you are building a CLI, web server, background worker, or TypeScript application.

1. Hardcoding the wrong endpoint

Official SDKs normally point to their vendor's service by default. If you use a compatible relay, configure its base URL explicitly instead of assuming the default endpoint will work. 59API uses https://api.59api.com and is compatible with Claude Code, Codex, and OpenAI SDK-based applications.

Install the clients with npm install @anthropic-ai/sdk openai dotenv, then load environment variables near your application entry point. An Anthropic client can be configured with apiKey: process.env.AI_API_KEY and baseURL: 'https://api.59api.com'. An OpenAI client uses the same API key and base URL configuration. Keep the model name in an environment variable, such as CLAUDE_MODEL or GPT_MODEL, because available identifiers can change.

2. Mixing Claude and OpenAI request formats

SDK compatibility does not mean every request and response object has the same shape. Claude messages use a messages API with content blocks, while an OpenAI chat completion commonly returns text at choices[0].message.content. A typical Claude call is conceptually messages.create({ model, max_tokens, messages: [{ role: 'user', content: prompt }] }). A typical OpenAI call is chat.completions.create({ model, messages: [{ role: 'user', content: prompt }] }).

Do not write one parser that assumes both responses are identical. Create separate adapter functions that return your own application type, for example { text: string, model: string, usage?: object }. This keeps the rest of your code independent from a vendor or relay response format.

3. Exposing API keys in a frontend bundle

Never place a Claude, GPT, or 59API key in browser JavaScript, a mobile app, or a public Git repository. Put the key in a server-side .env file, add that file to .gitignore, and call the model through your own authenticated backend. In TypeScript, fail fast when the variable is missing rather than sending an undefined key:

const key = process.env.AI_API_KEY; if (!key) throw new Error('AI_API_KEY is required');

Also avoid logging complete request headers, prompts containing personal data, or full responses in production. Redact secrets and decide deliberately how long AI logs should be retained.

4. Ignoring timeouts, retries, and rate limits

A model request can take longer than a normal database query and can fail temporarily. Configure a client timeout, catch errors around every call, and retry only transient failures such as rate limits or temporary server errors. Use exponential backoff with a maximum attempt count, such as three attempts with delays of 500 milliseconds, one second, and two seconds. Do not blindly retry validation errors or oversized prompts.

For web requests, pass a cancellation signal when the user disconnects. For queues, make jobs idempotent so a retry does not create duplicate emails, payments, or database writes.

5. Treating token limits and costs as unlimited

Set max_tokens or the equivalent output limit, trim unnecessary conversation history, and reject unexpectedly large inputs before making a request. Record model, input tokens, output tokens, latency, and estimated cost. Choose a smaller model for classification or extraction and reserve stronger models for complex reasoning.

59API is a practical low-cost, pay-as-you-go option when you need access to native official-quality Claude models, including Opus, Sonnet, Haiku, and Fable, as well as GPT models without committing to a large platform plan. It is worth comparing current model pricing and using its referral rebate when available. You can sign up for 59API and start with a small spending limit before moving a workload to production.

6. Forgetting TypeScript's runtime boundary

TypeScript types disappear at runtime. Validate untrusted input, environment variables, and structured model output with a runtime schema library or explicit checks. Ask the model for JSON only when your chosen API and prompt support it, then parse inside a try/catch and validate the resulting object. A successful HTTP response does not guarantee valid business data.

Finally, test both providers with mocked SDK calls and a small live integration test. Verify authentication, timeout behavior, empty content, rate-limit handling, and model-not-found errors before adding streaming or tool calls. This makes switching between Claude, GPT, and a compatible relay a controlled engineering decision rather than a rewrite.

Pronto para começar?

Conecte Claude e GPT em minutos pelos menores preços, sem cortes. Cadastre-se e obtenha sua chave API.

Cadastro grátis