// Before you start
This guide builds on this one:
Nothing here is clever, which is the point. You are building a small, reliable base: the right tools installed, a secret kept safe, and one successful call to a model.
Install Node.js#
The examples use TypeScript. Install Node.js 20 or newer, then check the version in a new terminal window.
Windows
winget install OpenJS.NodeJS.LTS# close the terminal, open a new one, then:node --versionmacOS
brew install nodenode --versionLinux
# Easiest on most distributions: install nvm from nvm.sh, then:nvm install --ltsnode --versionNote
Prefer Python? Lesson 3 has every example in both languages. Everything else, including this setup, works the same.
Get an API key and keep it out of your code#
Create an account with a model provider and make an API key. This path uses the Anthropic API; the ideas work with any provider. A key is a password that costs you money if it leaks, so it never goes in a file you might share or commit.
Put it in an environment variable instead. Your code reads it from there, and the SDK looks for the name ANTHROPIC_API_KEY automatically.
Windows
# PowerShell. Lasts for this window only:$env:ANTHROPIC_API_KEY = "paste-your-key-here"# Or save it for every new window:setx ANTHROPIC_API_KEY "paste-your-key-here"macOS
# Lasts for this window only. To keep it, add the line to ~/.zshrcexport ANTHROPIC_API_KEY="paste-your-key-here"Linux
# Lasts for this window only. To keep it, add the line to ~/.bashrcexport ANTHROPIC_API_KEY="paste-your-key-here"Heads up
If you ever paste a key into a chat, a screenshot or a public repository, treat it as leaked: delete it in the provider's dashboard and make a new one. Do it straight away, not later.
Create the project#
npm
mkdir notes-agent && cd notes-agentnpm init -ynpm pkg set type=modulenpm install @anthropic-ai/sdknpm install -D tsx typescript @types/nodepnpm
mkdir notes-agent && cd notes-agentpnpm initpnpm pkg set type=modulepnpm add @anthropic-ai/sdkpnpm add -D tsx typescript @types/nodeMake your first call#
Create a file called hello.ts and paste this in. It sends one message and prints the reply.
import Anthropic from '@anthropic-ai/sdk'const client = new Anthropic() // reads ANTHROPIC_API_KEY from your environmentconst reply = await client.messages.create({ model: 'claude-sonnet-5-5', max_tokens: 200, messages: [{ role: 'user', content: 'Say hello in five words.' }],})const block = reply.content[0]console.log(block.type === 'text' ? block.text : block)Run it with npx tsx hello.ts. If you see five friendly words, your setup works and you can stop worrying about it for the rest of the path.
Note
Model names change over time. If you get a "model not found" error, check the provider's current model list and swap the name in the code. Nothing else needs to change.
// Try it
Break it on purpose
Errors are much less scary when you have seen them before. Make this one happen deliberately:
- Open a new terminal window, so the key you set earlier is gone.
- Run
npx tsx hello.tsagain. - Read the error message. What is it telling you, in your own words?
Show a solutionHide the solution
// Solution
You should see an authentication error: the SDK could not find an API key, so the provider refused the request. Nothing is wrong with your code.
Fix it by setting ANTHROPIC_API_KEY again in that window (or use setx / your shell profile so every new window has it). Remember that a variable set with setx only appears in windows opened afterwards.