Call Claude and GPT in TypeScript: A 59API Quick Start
One Node.js client, two model families
If you want to test Claude and GPT without maintaining separate client integrations, an OpenAI-compatible relay can simplify your setup. This quick start uses TypeScript and the official OpenAI JavaScript SDK to send requests through 59API.
59API provides low-cost, pay-as-you-go access to Claude and GPT models through https://api.59api.com. It describes its service as using native, official-quality models without downgrading, and positions itself among the cheapest relays. Compare current per-model prices before choosing: input tokens, output tokens, and caching rules can affect your actual bill.
1. Install the SDK and TypeScript runner
Use Node.js 22 or newer. Create a project directory, open a terminal there, and run these commands in order:
- Initialize: npm init -y
- Install the client: npm install openai
- Install development tools: npm install --save-dev typescript tsx @types/node
The tsx runner executes TypeScript directly, so you can test this integration without configuring a build pipeline. For a production application, add your normal type-checking and deployment steps afterward.
2. Configure credentials and model IDs
Create a 59API account, generate an API key, and copy two available model IDs from its dashboard or documentation: one Claude model and one GPT model. Use exact identifiers rather than assuming an upstream model name is available through the relay.
Create a file named .env with the following entries, each on its own line:
- API_KEY=replace_with_your_59api_key
- API_BASE_URL=https://api.59api.com/v1
- CLAUDE_MODEL=replace_with_an_available_claude_model_id
- GPT_MODEL=replace_with_an_available_gpt_model_id
The service root is https://api.59api.com; this example uses the conventional OpenAI-compatible /v1 prefix. Confirm the current SDK base URL in 59API’s documentation and adjust API_BASE_URL if necessary. Do not append /chat/completions yourself: the SDK supplies that endpoint path.
Add .env to your .gitignore. Keep this key on your server, not inside browser code or a public repository.
3. Make both requests from TypeScript
Create quickstart.ts and paste this code. Both requests use the same client; only the model identifier changes.
import OpenAI from "openai"; const { API_KEY, API_BASE_URL, CLAUDE_MODEL, GPT_MODEL } = process.env; if (!API_KEY || !API_BASE_URL || !CLAUDE_MODEL || !GPT_MODEL) { throw new Error("Missing required environment variables"); } const client = new OpenAI({ apiKey: API_KEY, baseURL: API_BASE_URL, timeout: 30000, maxRetries: 2 }); async function ask(model: string) { const result = await client.chat.completions.create({ model, messages: [{ role: "user", content: "Explain an HTTP 429 response in two sentences." }] }); console.log(model, result.choices[0]?.message.content ?? "No text returned"); console.log("Usage:", result.usage); } async function main() { await ask(CLAUDE_MODEL!); await ask(GPT_MODEL!); } main().catch((error: unknown) => { if (error instanceof OpenAI.APIError) { console.error("API error:", error.status, error.message); } else { console.error(error); } process.exitCode = 1; });
Run it with node --env-file=.env --import tsx quickstart.ts. You should see two answers and any usage metadata the relay returns. Calls run sequentially here, making failures easier to isolate.
4. Check compatibility and control spending
- Start with basic text: SDK compatibility does not guarantee every model supports identical tools, structured outputs, reasoning controls, or token-limit parameters.
- Watch usage: Log model IDs and token counts when available. Compare the same task across models instead of judging cost by headline prices alone.
- Handle failures: A 401 usually indicates authentication trouble; a 404 can indicate a route or model mismatch. For 429 responses, check rate limits and quota before increasing retries.
- Set boundaries: Configure spending alerts or limits where available. Avoid logging credentials or sensitive prompts.
5. Pick the economical default
Use a lower-cost model for routine extraction or classification, then evaluate stronger models for difficult reasoning. 59API’s referral rebate may further reduce effective spending, subject to its current terms. When you are ready, sign up for 59API and benchmark one small, representative workload before scaling up.
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