Guides / Prompting and shipping

How to Integrate the Claude API into Your App

Add Anthropic's Claude API to your app: API keys, the Messages API, system prompts, streaming, errors like 429 and 529, spend limits and safe server-side calls.

Mythex Team · 2026-09-29 · 6 min read

To integrate the Claude API, create an API key in Anthropic's Claude Console, store it as a server-side secret (usually ANTHROPIC_API_KEY), and call the Messages API from a route on your own server using one of Anthropic's official SDKs. Each request names a model, sets max_tokens (the longest reply you'll accept) and sends the conversation as messages, with your instructions in a separate system prompt. Set a spend limit, stream long replies, and handle rate-limit (429) and overload (529) errors with retries.

This guide covers setup, a safe request flow, prompts for an AI builder, and the errors to plan for. Anthropic details are from its documentation as of September 2026. For how tokens and pricing work in general, see how to use LLM APIs.

What you can build with it

  • Chat assistants and help widgets (how to build an AI chatbot app)
  • Summaries, rewrites and drafted replies
  • Extracting fields from documents, emails and forms
  • Answering questions from your own content
  • Agents that call your app's functions through tool use

For more ideas, see how to add AI features to your app.

The Messages API in brief

According to Anthropic's API reference, a request is a POST to https://api.anthropic.com/v1/messages with:

PartWhat it is
x-api-key headerYour API key
anthropic-version headerThe API version, for example 2023-06-01
model (required)Which Claude model to use
max_tokens (required)The most tokens the reply may contain; the model may stop earlier
messages (required)The conversation: alternating user and assistant turns
system (optional)Instructions that apply to the whole conversation
stream (optional)Set to true to receive the reply as it's written, via server-sent events

The API is stateless: to continue a conversation, send the earlier turns again with each request. That's why long chats cost more per message.

Anthropic publishes official SDKs for Python, TypeScript, C#, Go, Java, PHP and Ruby. They set the headers, parse responses, and support streaming. Use one instead of raw HTTP unless you have a reason not to.

Choosing a model

Anthropic offers several Claude models that trade capability against speed and price, and the line-up changes. Pick the smallest model that does your task well, test it on real examples, and keep the model ID in configuration so you can switch without a code change. Check Anthropic's models and pricing pages for current options and rates.

Setting up your account

  1. Sign in to the Claude Console at platform.claude.com and add billing.
  2. Create an API key. Keep one key per environment (testing and production) so you can revoke one without breaking the other.
  3. Set a spend limit. Anthropic's errors page notes that when usage reaches a spend limit you've set, the API returns a 400 error, so your app should show a clear message when that happens.
  4. Review rate limits for your account in the Console; they're measured per organization.

The request flow

  1. The user acts in your app: sends a message, clicks "Summarise".
  2. The browser calls your server route, never Anthropic directly.
  3. Your server checks the user is logged in and allowed to use the feature, and applies a per-user limit.
  4. It builds the request: a system prompt with your rules, the user's input, and any data the task needs from your database.
  5. It calls the Messages API with the key from the environment and a sensible max_tokens.
  6. It streams the reply to the browser and logs token usage from the response.

Options and trade-offs

DecisionSimple choiceWhen to go further
StreamingWait for the full replyStream for chat and anything long; Anthropic recommends streaming or batches for long-running requests
Output formatPlain textStructured JSON output when your code needs to read fields
Your dataInclude the relevant recordRetrieval over your content when there's too much (what RAG is)
ActionsModel only writes textTool use, where the model asks your code to run a function you define
Bulk, non-urgent workLoop over itemsThe Message Batches API, which you submit and poll for results

Prompts to give your AI builder

A first feature:

Add an "Ask about this document" panel. It calls a server route POST /api/ask that checks the user is logged in and owns the document, then calls the Claude Messages API with the official Anthropic SDK and the secret ANTHROPIC_API_KEY. Read the model ID from the secret CLAUDE_MODEL. System prompt: "Answer only from the document provided. If the answer isn't in it, say so." Send the document text and the question, set max_tokens to 800, stream the answer to the page, and never expose the key to the browser.

Guardrails and errors:

Limit each user to 20 questions per hour, tracked in the database. Log input and output tokens from each response with the user ID and the request ID. On 429 or 529 errors, retry with exponential backoff up to 3 times, respecting any retry-after header, then show "The assistant is busy, try again shortly." If the API reports a spend limit error, show "AI features are paused" and alert the admin.

Errors to plan for

From Anthropic's errors documentation, the ones your app will actually meet:

CodeTypeWhat to do
400invalid_request_errorFix the request; also returned when you hit a spend limit you set
401authentication_errorCheck the key is present, correct and not revoked
413request_too_largeSend less; the Messages API accepts up to 32 MB per request
429rate_limit_errorBack off and retry, respecting retry-after
500api_errorRetry with backoff; contact support with the request ID if it persists
529overloaded_errorTemporary high traffic across all users; back off and retry

Anthropic's docs say the official SDKs automatically retry transient failures (connection errors, rate limits and 5xx errors) twice by default with exponential backoff, and you can configure that. Every response carries a request-id header; log it so support can trace problems.

Common mistakes

  • Calling the API from the browser. Your key becomes public. Always go through your server. See how to keep API keys safe.
  • Forgetting max_tokens is a cap, not a target. Set it to what the feature needs; too low truncates answers, too high risks long, costly replies.
  • Putting rules in the user message. Stable instructions belong in the system prompt; permissions belong in your code.
  • Sending the whole chat forever. Trim or summarise old turns as conversations grow.
  • No spend limit or per-user cap.
  • Hard-coding a model ID. Models get updated and retired.
  • Ignoring the stop reason. If the reply stopped because it hit max_tokens, tell the user or raise the cap.

Checklist

  • API key created in the Claude Console and stored as a server-side secret
  • Separate keys for testing and production
  • Spend limit set; clear message when it's reached
  • All requests go through your server with permission checks
  • System prompt holds the rules; model ID is configurable
  • max_tokens set per feature; long replies streamed
  • Per-user rate limit in your app
  • 429 and 529 handled with backoff; request IDs logged
  • Friendly error state that doesn't break the page
  • Tested with empty, very long and hostile inputs

Using the Claude API in Mythex

In apps you build on Mythex, Claude features use your own Anthropic key and are billed to your Anthropic account, not to Mythex build credits. That's separate from the Mythex agent that writes your code. Add ANTHROPIC_API_KEY as a project secret through /secrets, project settings or the secrets card in chat, then describe the feature using prompts like the ones above. Publish again after changing secrets on a live app. See Add AI features with your own API keys for a starter prompt. If you're choosing between providers, how to integrate the OpenAI API covers the same ground for OpenAI.

Questions

How do I get a Claude API key?

Sign in to the Claude Console at platform.claude.com, set up billing, and create an API key. Store it as a server-side secret, conventionally named ANTHROPIC_API_KEY, and never put it in browser code.

What does a request to the Claude API need?

As of September 2026, Anthropic's Messages API reference lists three required body fields: model, max_tokens and messages. Direct HTTP calls also need the x-api-key and anthropic-version headers; the official SDKs set these for you.

What is a 529 error from the Claude API?

Anthropic's errors page describes 529 as overloaded_error: the API is temporarily overloaded because of high traffic across all users. Retry with exponential backoff. The official SDKs retry some transient errors automatically.

Can I use Claude with code written for the OpenAI SDK?

Anthropic documents an OpenAI SDK compatibility layer for using Claude through the OpenAI SDK surface. For new code, its own SDKs give access to Claude-specific features, so they are usually the better starting point.

Keep reading

  • How to Add a Blog to Your Website: Options, SEO and Setup — How to add a blog to your website: Markdown files vs a built-in editor vs a CMS, subfolder vs subdomain, SEO basics, and prompts to build it with AI.
  • How to Add a Contact Form to Your Website (That Actually Reaches You) — How to add a contact form that works: save messages, get email alerts, stop spam, and avoid the mistakes that silently lose enquiries. Prompts included.
  • How to Add a Database to Your App (Without Losing Data Later) — How to add a database to an app you built: when you need one, Postgres vs hosted options, designing tables, prompts to use, and mistakes that lose data.
  • How to Add AI Features to Your App — Add summaries, chat, data extraction and classification to your app with an LLM API — keeping keys safe, costs under control and output trustworthy.
  • How to Add Analytics to Your App: GA4, Privacy-First Tools and Product Analytics — How to add analytics to your website or app: Google Analytics 4 vs privacy-first vs product analytics, what to track, cookie consent, and prompts to use.
  • How to Add Cookie Consent to Your Website — Add a cookie banner that actually blocks scripts until people agree: what needs consent, CMP vs custom, Google consent mode and a checklist. Not legal advice.

Start building free · Templates · Docs