Your First AI Project

Lesson 3 of 14

Your first API call

An API is how one program asks another program for something. When you use a chatbot in a browser, the page is doing exactly what you are about to do in code — sending your text to a model over the internet and displaying what comes back. The difference is that now you control what gets sent and what happens to the reply.

The key, and why it is a password

To use a model API you need an API key: a long string that identifies your account and gets billed for what you use.

Get one from your provider's console — console.anthropic.com for Claude, platform.openai.com for OpenAI. Both require a payment method and a small amount of credit. Both show the key exactly once, at creation.

Treat it as a password, because it functions as one. Anyone holding it can spend your money. There is one rule that follows and it is not optional:

Never type your key into a code cell.

Not "usually not". Never — because notebooks get shared, screenshotted, copied into a message, and saved to Drive with their contents intact. Keys leaked this way are found by automated scanners within minutes of being posted anywhere public, and the bill is yours.

Colab secrets, which solve this properly

Colab has a key icon in the left sidebar. Click it, add a secret named ANTHROPIC_API_KEY, paste the value, and enable notebook access.

Now the key lives in your Colab account, not in the notebook. Share the notebook and the key does not travel with it.

python
from google.colab import userdata
api_key = userdata.get('ANTHROPIC_API_KEY')
print("key loaded:", api_key[:7] + "…")
text
key loaded: sk-ant-…

Printing the first seven characters confirms it loaded without displaying anything usable — a small habit worth keeping, because "is my key actually loaded?" is a question you will ask often and should never answer by printing the key.

The call

python
!pip install anthropic

Then:

python
from anthropic import Anthropic

client = Anthropic(api_key=api_key)

response = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=300,
    messages=[
        {"role": "user", "content": "In one sentence, what is an API key?"}
    ],
)

print(response.content[0].text)

That is the entire thing. Six lines of substance, and everything else in this course is variations on it.

Read what each piece is doing, because these five choices are the whole interface.

client holds your key so you do not pass it on every call.

model picks which model. Bigger models are more capable and cost more; lesson 13 covers choosing.

max_tokens caps the length of the reply. It is a safety limit as much as a setting — it is what stops a runaway response costing real money. Set it to roughly what you expect and no more.

messages is the conversation, as a list. Each entry has a role and content. Right now there is one message from "user". Lesson 5 adds the other kind.

response.content[0].text digs the text out of the reply object, which is the subject of the next lesson.

One run of the above returned:

text
An API key is a unique string of characters that authenticates your identity to a service, letting it verify who you are and track or bill your usage.

Yours will be worded differently. That is not a bug; it is what "probabilistic" means, and lesson 5 discusses when you want to reduce it.

If it fails

AuthenticationError — the key is wrong, or has a stray space from pasting. Re-copy it.

NotFoundError mentioning the model — that model name is not available on your account. Check the provider's model list; names change.

RateLimitError on your very first call — usually means no credit on the account rather than too many requests.

NameError: name 'api_key' is not defined — you did not run the cell that loads the secret, or the runtime restarted. Run from the top.

Every one of these is ordinary and every one is fixed in under a minute. Lesson 9 makes reading them routine.

Do this today: get a key, put it in Colab secrets, and make one call. When the reply prints, you have done the thing this whole course is built on — everything after this is shaping what goes in and what happens to what comes out.

← Previous