Ch. 16 — AI API Integration
Chapter 16 Phase 2 — UX Engineer

Building with AI APIs

You've been using Claude. Now wire it into your own product.

⏱ 1.5 hours 🛠 VS Code · Anthropic account · Vercel

From user to builder

You've been using Claude as a tool — ask a question, get an answer. This chapter is about a different relationship: you're building a product and Claude is a feature inside it. Your users will interact with AI without knowing they're talking to Claude.

This is what the next generation of software looks like. Not just AI-assisted development, but AI-powered products. And as a UX Engineer, you're positioned to build them.

API keys — what they are and how to keep them safe

An API key is a secret credential that authenticates your requests to a service. Think of it as a password for your application. The Anthropic API key looks like sk-ant-api03-...

⚠️ Critical
Never put your API key in client-side JavaScript
If your API key is in your React component or frontend JavaScript, anyone can open DevTools, find it, and use it on your bill. Always make API calls from a serverless function (like the one you learned in Chapter 15). The key lives on the server, never in the browser.

The Claude API — messages and responses

The Anthropic API is simple. You send an array of messages (just like a conversation), and you get a response. Here's the minimal request:

javascript — serverless function (api/ask.js)
export default async function handler(req, res) {
  const { userMessage } = req.body;

  const response = await fetch('https://api.anthropic.com/v1/messages', {
    method: 'POST',
    headers: {
      'x-api-key': process.env.ANTHROPIC_API_KEY,
      'anthropic-version': '2023-06-01',
      'content-type': 'application/json',
    },
    body: JSON.stringify({
      model: 'claude-opus-4-6',          // the model to use
      max_tokens: 1024,                   // max response length
      system: 'You are a helpful product design assistant. Be concise.',
      messages: [
        { role: 'user', content: userMessage }
      ]
    })
  });

  const data = await response.json();
  const reply = data.content[0].text;
  res.json({ reply });
}
javascript — frontend calling the API route
// In your React component or vanilla JS
async function askClaude(question) {
  const response = await fetch('/api/ask', {
    method: 'POST',
    headers: { 'content-type': 'application/json' },
    body: JSON.stringify({ userMessage: question })
  });
  const data = await response.json();
  return data.reply;
}

System prompts — giving Claude a job

The system parameter lets you define Claude's role before the conversation starts. This is where you give it a persona, constraints, and context. It's the most powerful tool for shaping AI behavior in your product.

javascript — system prompt examples
// For a writing feedback tool
system: `You are a UX writing expert. Review the user-submitted copy and give
concise feedback on: clarity, action orientation, tone, and length.
Format your response as 3-5 bullet points. Be direct, not fluffy.`

// For an onboarding assistant
system: `You are an onboarding guide for a project management tool.
Help new users understand features. Keep responses under 100 words.
If asked about something unrelated to project management, politely redirect.`

// For an ideas board
system: `You are a product thinking partner. The user will share a product idea.
Respond with: one strength, one potential user problem, and one next step to validate.
Keep the whole response under 80 words.`

Cost considerations

The Claude API is priced per token (roughly per word). For small hobby projects, costs are negligible — a few cents per day of active usage. For products with real users, set a budget and monitor your dashboard.

  • claude-haiku-4-5 — fastest and cheapest. Great for high-volume, shorter tasks (classification, summaries, quick answers).
  • claude-sonnet-4-6 — balanced speed and capability. Best for most product features.
  • claude-opus-4-6 — most capable. Best for complex reasoning tasks where quality matters most.
  • Set max_tokens appropriately — don't leave it at 4096 if you only need 200-word responses.
🤖
Hands-on activity
Add AI feedback to your ideas board
You need: VS Code Anthropic API key (console.anthropic.com) Vercel project from Ch. 15 Claude (for code generation)
  1. 1
    Get an Anthropic API key from console.anthropic.com. Add it as an environment variable in your Vercel project: Settings → Environment Variables → ANTHROPIC_API_KEY.
  2. 2
    Ask Claude: "Create a Vercel serverless function at api/feedback.js that takes a POST request with a JSON body {idea: string}. It calls the Anthropic API using the model claude-haiku-4-5-20251001, with a system prompt that says you are a product thinking partner who gives 3-bullet feedback on ideas (one strength, one risk, one next step). Return the reply as JSON {feedback: string}."
  3. 3
    Add a "Get AI feedback" button to each idea card in your HTML. When clicked, POST the idea text to /api/feedback and display the response below the idea.
  4. 4
    Deploy the updated code to Vercel. Test it in production. You've shipped a real AI-powered feature.
Key takeaways
  • Never put API keys in client-side JavaScript. Always call AI APIs from serverless functions
  • The Claude API is a simple POST request: send messages, get a response
  • System prompts define Claude's role, constraints, and output format — craft them carefully
  • Use claude-haiku-4-5 for high-volume tasks, claude-sonnet-4-6 for balanced quality/cost
  • Set max_tokens intentionally to control response length and cost