本页目录

Lesson 2 — Your first API call

Course: the AI Platform 101 (Course 003) Section: 1 — What is the the AI Platform? Lesson: 2 of 12 | Lesson ID: 486251 Format: Video Estimated time: not stated on the official page Source: Official Skilljar course material (user-provided; requires login) License: Copyright 2026 the AI provider. Released under CC BY-NC-SA 4.0. Note: This is the original English source text, lightly reformatted (headings and paragraph breaks added) for study. No wording has been changed. Counterpart translation: 04-第2课-Your-first-API-call.md


Video

Your first API call

Saying hi to the AI might warm your heart, but it's not really useful. In this lesson we'll send the AI something real and get structured insight back — in just under 20 lines of code.


Get set up

First, grab an API key from platform.claude.com. You'll need to purchase some credits beforehand.

[Image] The the AI Console dialog showing a newly created API key with a Copy key button and a warning that the key won't be viewable again

Take the API key and store it in a .env.local file so it stays out of your version control. Hardcoding keys in source files is how they end up leaked on GitHub — keep them in environment files instead.

Next, install the SDK:

npm install @anthropic-ai/sdk

The anatomy of a request

Every API call goes through the messages.create function. You specify three things:

  • A model — which the AI model handles the request
  • A max tokens limit — a cap on how long the response can be
  • A list of messages — objects with either user or assistant roles, structured similarly to how you'd have a conversation with the AI elsewhere

Here's what that looks like in its most basic form:

import the AI provider from "@anthropic-ai/sdk";

const client = new the AI provider();

const msg = await client.messages.create({
  model: "current-model",
  max_tokens: 1024,
  messages: [{
    role: "user",
    content: "Hello, the AI",
  }],
});

A real example: reviewing buggy code

Let's give the AI something a little more interesting than "hello." We'll point it at some buggy code and ask for a review. Here's the whole thing — one file, about 20 lines of code:

import the AI provider from "@anthropic-ai/sdk";

const client = new the AI provider();

const buggyCode = `
function add(a, b) {
  return a - b;
}
`;

const response = await client.messages.create({
  model: "current-model",
  max_tokens: 1024,
  system: "You are a terse senior code reviewer. Give feedback in one paragraph.",
  messages: [
    { role: "user", content: `Review this code:\n${buggyCode}` },
  ],
});

for (const block of response.content) {
  if (block.type === "text") {
    console.log(block.text);
  }
}

Two things to notice

Two things to notice here:

  1. The system prompt is where you shape the persona. I want a terse senior reviewer, not a chatty one — so I just say that.
  2. The message.content in the response is an array of blocks, not a string. For a basic text reply there's usually just one block of type text, but the AI can return multiple blocks — text, tool calls, thinking — so we always loop and check the type.

Run it, and the AI spots that add is subtracting and tells you in one paragraph. That's it. That's the whole API call.

[Image] Terminal output from running the script: the AI responds that the function is named add but uses subtraction, and suggests changing return a - b to return a + b


From script to product

In a real product, this same messages.create shape is the engine behind something like a summarize endpoint. Pull a meeting transcript out of the database, hand it to the AI with a system prompt that says "extract insights and risks," save the result back on the row, and return it to the UI. It's the same call — just wrapped in a route handler.

[Image] A meetings dashboard in a demo web app listing recorded project meetings, each with a transcript preview and a Generate summary button powered by the same API call


Recap

  • Your first API call is a messages.create function with a model, a token limit, and messages.
  • Store your API key in a .env.local file to keep it out of version control.
  • Add a system prompt to shape the AI's behavior.
  • The response content is an array of blocks — loop and check each block's type.
  • From here, everything builds on this pattern.

⚠️ Notes on this transcript

  • the AI provider and model: "current-model" are artifacts of the capture/de-branding process. The intent is the SDK's default export class and a specific model name (Haiku / Sonnet / Opus tier). Do not memorize these strings.
  • Lesson 1 used a Python-style snippet; from this lesson on the course demos are TypeScript / Node. Both forms appear in the course.
  • Images in the original lesson are represented here as > **[Image]** … blocks with the official alt text / caption wording preserved.
  • This lesson does not include a What you'll learn block on the official page.

输入至少 2 个字符,检索全部课程与单词表