Tutorials · Intermediate

How to connect an AI API (step by step)

Learn to connect an AI API from scratch: create your API key, install the SDK, store the key safely and make your first call in Python or Node.js.

  • Claude API
  • Python
  • Node.js

An AI API is how your own program talks to a model like Claude or GPT: your code sends a question and gets the answer back, without you typing in a website. It’s the step that separates “I use AI” from “I build with AI”. In this guide you’ll connect an AI API from scratch: create your key, install the SDK and make your first call.

Pick your system (Windows or Mac) with the buttons above and tap each step to open it.

Before you start: an API is billed per use (by tokens), not a monthly fee. To practice you’ll spend cents. See costs in the right column.

1 Create your account and API key

We’ll use the Claude (Anthropic) API as the example. The pattern is the same for OpenAI and others.

  1. Go to https://console.anthropic.com and create your account.
  2. In the left menu open API Keys.
  3. Click Create Key, give it a name (e.g. “testing”) and copy it.

The key starts with sk-ant- and is shown only once. Save it somewhere safe; if you lose it, create another.

2 Add credit (billing)

The key won’t work until you add a payment method and a little credit.

  1. In the console open Settings → Billing.
  2. Add a card and buy a small credit ($5 is plenty to practice).

It’s prepaid: you spend against that balance. You can set a monthly limit so there are no surprises.

3 Install the SDK

The SDK is a library that makes talking to the API easy. Pick your language. You need PowerShellthe Terminal open.

With Python (you need Python installed):

pip install anthropic

With Node.js (you need Node.js installed):

npm install @anthropic-ai/sdk

Prefer OpenAI? It’s pip install openai or npm install openai. The rest is almost the same.

4 Store your API key safely

Never write the key inside your code: anyone who sees the file (or your GitHub repo) would have it. Instead, store it as an environment variable.

In PowerShell (saved for your user):

setx ANTHROPIC_API_KEY "sk-ant-your-key-here"

Close and reopen PowerShell so it picks up the change.

In the Terminal (for the current session):

export ANTHROPIC_API_KEY="sk-ant-your-key-here"

To make it permanent, add that line to your ~/.zshrc file and run source ~/.zshrc.

The SDK reads that variable on its own, without you writing the key in the code.

5 Make your first call

Create a file and paste your language’s example. Change only the question text.

Python (test.py):

import anthropic

client = anthropic.Anthropic()  # reads ANTHROPIC_API_KEY from the environment

message = client.messages.create(
    model="claude-haiku-4-5",
    max_tokens=300,
    messages=[
        {"role": "user", "content": "Explain in one sentence what an API is."}
    ],
)

print(message.content[0].text)

Run:

python test.py

Node.js (test.mjs):

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

const client = new Anthropic(); // reads ANTHROPIC_API_KEY from the environment

const message = await client.messages.create({
  model: "claude-haiku-4-5",
  max_tokens: 300,
  messages: [
    { role: "user", content: "Explain in one sentence what an API is." },
  ],
});

console.log(message.content[0].text);

Run:

node test.mjs

If you see the answer printed in the terminal, you just connected your first AI API.

Model names (like claude-haiku-4-5) change over time. Confirm the current one in the official docs.

If something goes wrong
ProblemFix
authentication_error / 401The key isn’t being read. Check the environment variable (Step 4) and reopen the terminal.
credit balance is too lowAdd credit in Settings → Billing (Step 2).
model not foundThe model name changed; confirm the current one in the official docs.
command not found: pip/nodeYou still need to install Python or Node.js (Step 3).
Security: protect your key
  • Never write it in the code or upload it to GitHub (use environment variables).
  • If you work in a project, add your keys file (e.g. .env) to .gitignore.
  • If you think it leaked, delete it from the console and create a new one.
  • Set a monthly spending limit so you can sleep easy.

Ready for the next step? Learn to create your first AI agent using what you just connected.

Frequently asked questions

What is an AI API?

It's the door for your own program to talk to an AI model. Instead of typing in a website like ChatGPT or Claude, your code sends a question and gets the answer back, to automate or build apps.

Do I need to know how to code to connect an AI API?

A little helps, but you can copy the example in this guide and change only the question text. That already gets your first call working.

How much does using an AI API cost?

You pay per use, by tokens (chunks of text), not a monthly fee. For testing it's very cheap: a fast model like Claude Haiku costs around $1 per million input tokens. Confirm current prices on the official page.

Is it safe to put my API key in the code?

No. Store it as an environment variable (Step 4) and never upload it to GitHub. If it leaks, delete it from the console and create a new one.

Should I use Python or Node.js?

Either one works; this guide shows both. If you've never coded, Python is usually easier to read to start.