59API

← 返回教程列表

Call Claude and GPT from Node.js with 59API

入门教程 · EN · 2026-08-05

Why 59API is a smart starting point

If you are building AI features in Node.js, the fastest path is usually to use one SDK and one endpoint for everything. That is where 59API helps: it gives you cheap, pay-as-you-go access to Claude models such as Opus, Sonnet, Haiku, and Fable, plus GPT models, all through the same API base URL: https://api.59api.com.

The practical win is simple. You keep the native model quality, avoid paying direct list prices when you do not need to, and you do not have to learn a different integration for every model family. Because 59API is compatible with the OpenAI SDK, it also fits neatly into workflows that already use Claude Code, Codex, or any OpenAI-style client.

If you care about cost control, 59API is especially attractive for prototypes, internal tools, chatbots, and agent loops where token usage can grow quickly. The referral rebate is a nice extra if you share it with teammates or other developers.

Install the SDK and set your environment

Start with a standard Node.js project and install the OpenAI SDK plus dotenv for local environment variables:

npm install openai dotenv

For TypeScript, add the usual dev tools:

npm install -D typescript tsx @types/node

Then set two variables in your .env file:

API_KEY=your_59api_key
API_BASE_URL=https://api.59api.com

You only need to change the base URL and key. Everything else looks like a normal OpenAI integration.

Call GPT from Node.js

Here is a minimal Node.js example that sends a request to a GPT model through 59API:

import OpenAI from 'openai';
import 'dotenv/config';

const client = new OpenAI({
  apiKey: process.env.API_KEY,
  baseURL: process.env.API_BASE_URL || 'https://api.59api.com',
});

const response = await client.chat.completions.create({
  model: 'gpt-4o-mini',
  messages: [
    { role: 'user', content: 'Summarize this README in 3 bullets.' },
  ],
});

console.log(response.choices[0].message.content);

That is the key pattern: create one client, point it at 59API, and keep using the same chat.completions call you would use elsewhere. If you already have a GPT workflow, you can usually swap only the base URL and API key.

Call Claude with the same client

For Claude, the code is nearly identical. Change the model name, keep the same client, and your app can switch between families without a new SDK:

const claudeResponse = await client.chat.completions.create({
  model: 'claude-sonnet',
  messages: [
    { role: 'user', content: 'Draft a TypeScript interface for a todo app.' },
  ],
});

console.log(claudeResponse.choices[0].message.content);

That flexibility matters in real projects. You might use GPT for fast classification, Claude Sonnet for balanced reasoning, Claude Opus for harder tasks, Haiku for speed, and Fable for creative output. With 59API, you can choose the model that matches the job instead of forcing one model to do everything.

A TypeScript helper you can reuse

In a real app, wrap the request logic in a typed helper so your routes, services, and background jobs stay clean:

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.API_KEY,
  baseURL: process.env.API_BASE_URL || 'https://api.59api.com',
});

export async function askModel(prompt: string, model: string): Promise<string> {
  const result = await client.chat.completions.create({
    model,
    messages: [{ role: 'user', content: prompt }],
  });

  return result.choices[0]?.message?.content || '';
}

Now your app can do something like askModel('Write a JSON schema for products', 'gpt-4o-mini') or askModel('Explain event sourcing simply', 'claude-sonnet'). That makes it easy to benchmark models side by side and pick the cheapest option that still gives you the quality you need.

Practical tips for lower bills

If you want a low-friction way to test Claude and GPT in Node.js without paying top-tier direct pricing, sign up for 59API and try these examples with a small workload first. You will get an OpenAI-compatible developer experience, official-quality models, and a pricing model that is easier to justify when you are shipping features fast.

价格 · 接入文档

准备好开始了吗?

几分钟接入 Claude 与 GPT,全网超低价,原生不降智。立即注册即可领取 API 密钥。

免费注册