← Blog

Claude API SDK npm Install Guide

2026-10-22 · 4 min read · SubToAPI Team

If you're searching for how to install the Claude API SDK with npm, here's the short answer: run npm install @anthropic-ai/sdk in your Node.js project, set your API key as an environment variable, and import the client with a few lines of code. The whole process takes less than five minutes if your environment is already set up correctly.

This guide walks through the full installation process step by step, covers the common errors developers hit (module resolution issues, missing API keys, Node version mismatches), and shows how to make your first request. It also covers an alternative path if you want a REST-only setup without managing SDK versions directly.

Prerequisites

Before installing anything, make sure you have:

If you're not sure which Node version you have, run:

node -v
npm -v

If node -v shows anything below v18, upgrade first — older versions can throw cryptic fetch is not defined errors when the SDK tries to make a request.

Step 1: Install the SDK

In your project directory, run:

npm install @anthropic-ai/sdk

For TypeScript projects, no separate types package is needed — the SDK ships with its own type definitions. If you're using yarn or pnpm:

yarn add @anthropic-ai/sdk
# or
pnpm add @anthropic-ai/sdk

Verify the install worked by checking your package.json dependencies list and confirming a node_modules/@anthropic-ai/sdk folder exists.

Step 2: Set your API key

Never hardcode your key in source files. Store it as an environment variable instead:

export ANTHROPIC_API_KEY="your-key-here"

Or in a .env file loaded with a package like dotenv:

npm install dotenv
import "dotenv/config";

The SDK automatically reads ANTHROPIC_API_KEY from the environment if you don't pass a key explicitly, which keeps your initialization code clean.

Step 3: Initialize the client

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

Step 4: Make your first request

async function main() {
  const message = await client.messages.create({
    model: "claude-opus-4-20250514",
    max_tokens: 1024,
    messages: [{ role: "user", content: "Explain npm in one sentence." }],
  });
  console.log(message.content);
}

main();

Run it:

node index.js

If everything is configured correctly, you'll see a response object with the model's reply in the content array.

Common installation errors

"Cannot find module '@anthropic-ai/sdk'" — Usually means the install didn't complete or you're running the script from the wrong directory. Re-run npm install and confirm node_modules is present in the same folder as your script.

"fetch is not defined" — Your Node version is too old. Upgrade to Node 18+.

"401 Unauthorized" on first request — Your API key isn't being read. Double-check the environment variable name matches exactly, and that .env is loaded before you initialize the client.

Type errors in TypeScript — Make sure your tsconfig.json targets ES2020 or later and moduleResolution is set to node or bundler, otherwise some SDK type exports won't resolve correctly.

When npm install isn't the right path

The official SDK is built for direct Anthropic API access with your own account, billing, and rate limits. That's the right choice if you're building a single app with one Claude account behind it.

But if you're trying to turn a single Claude subscription into something your whole team or multiple apps can call — with per-application API keys, usage tracking per key, and a dashboard instead of raw console logs — installing the SDK and wiring up auth yourself is more plumbing than most teams want to maintain.

That's the gap SubToAPI fills. It sits in front of your Claude access and gives you a standard HTTPS API with sub_live_... keys you can issue per app or per teammate, streaming support, tool use, and usage metadata — all without touching SDK version upgrades or auth internals. You still use fetch or any HTTP client; there's no separate SDK to install.

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-opus-4-20250514",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Explain npm in one sentence."}]
  }'

If you want to go this route instead of (or alongside) the npm SDK, the quickstart covers setup end to end, and the messages and streaming docs cover request patterns once you're past initial install. Plans start with a free trial at signup, and pricing details are on the pricing page.

Keeping the SDK updated

Once installed, check for updates periodically:

npm outdated @anthropic-ai/sdk
npm update @anthropic-ai/sdk

Pin the version in package.json if you want reproducible builds across environments, and review the changelog before upgrading in production — method signatures occasionally change between major versions.

Questions

Do I need to install anything besides the npm package to use Claude's API? No. The @anthropic-ai/sdk package includes everything needed to make requests — HTTP handling, types, and streaming support. You only need an API key and Node 18+.

Can I use the Claude API without installing an SDK at all? Yes. Both Anthropic's API and SubToAPI expose plain HTTPS endpoints, so you can use curl, fetch, or any HTTP client without installing a package. See the docs for request formats.

Why am I getting module resolution errors after installing? This is almost always a Node version or tsconfig.json misconfiguration. Confirm Node 18+ and that your TypeScript config targets ES2020 or later with moduleResolution set to node or bundler.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →