• Home

  • Productivity

  • App tips

App tips

15 min read

Claude API: How to get a key and use the API

By Miguel Rebelo · July 8, 2024
A hero image with the logo of Anthropic, the makers of Claude

When you're building your AI-powered app, you want the best output. But it's hard to objectively judge which model is better—especially if you want to build a user experience that feels warm and human. Coding and reasoning benchmarks aren't enough: sometimes, you need a model that "gets it" and can write in a natural-sounding way.

If OpenAI or Google models aren't giving you the warmth you need, you can turn to Anthropic's Claude. Marketed as the safest and most creative model, it's building a reputation for being a great writer. Even if you handle most of your app's reasoning with other models, using it to communicate with your users is a sure win.

In this guide, I'll give you a step-by-step tutorial on how to get a Claude API key, call the API, and get the responses where you want them.

If you're looking to connect the Claude models to the apps you use at work, you can do it without any API setup. Learn more about how Zapier can add AI to your existing workflows and how to connect Claude to Zapier.

Before you begin: How to use an API

I wrote this guide for people with a basic understanding of software, perhaps with some no-code experience. If this isn't you yet, you'll need more information to set up the API connections in this guide.

Start with Zapier's guide on how to use an API, which will explain everything about calls, methods, and responses. On top of that, you'll want to familiarize yourself with the basics of JSON, a data notation system that helps computers communicate with each other without running into errors. (Tip: Ask ChatGPT to explain it to you like you're 12.)

If everything is ringing bells so far, let's press on.

Table of contents:

  • What is the Claude API?

  • What can you do with the Claude API?

  • Claude API pricing

  • How to set up Claude API connections

  • Use Zapier to connect to Claude

What is the Claude API?

The Claude API lets you integrate Claude's AI models into your apps. You can send prompts to Anthropic (the company behind Claude) and get the outputs delivered to you wherever you need them: you can save them to your database, display them to your users, or use the output to run more complex logic.

Claude has three model families, in ascending order of quality and price:

  • Haiku, the fastest and most efficient model

  • Sonnet, the middle-weight for coding tasks and advanced reasoning

  • Opus, the expensive, most creative model out there

But as I'm writing this, the most powerful model is actually Claude 3.5 Sonnet, the one in the middle. Why's that? Sonnet was recently upgraded from version 3 to 3.5, while its siblings Haiku and Opus are still in version 3. That could change before you read this, so check out the model specifications page.

Did you enjoy Claude 2? No problem: you can still access the legacy API endpoints to run Claude 2.1, 2.0, and Instant models.

What can you do with the Claude API?

Text generation

The first and most obvious use of the Claude API is for generating text. You can write short- or long-form content, ask questions about something, or summarize a large chunk of text. The Claude models have a large context window (conversational memory) of 200,000 tokens, roughly 160,000 words, which can help you dig deep into big files.

Vision

Claude can't generate images, but it can see them. If you want to ask questions about any image, you can send over an image file along with the text prompt.

Embeddings

Using the embeddings API, you can improve the model's understanding of the context of your prompt so it can deliver better outputs. For example, imagine you're building an AI text adventure and want to continue the story with the same elements every time. If you use the embeddings to set it to "beach", "sunny day," and "pirates," whenever the user asks to continue the story, the output will always contain those elements instead of others.

Here's a slightly more technical explanation: when you pass "beach," "sunny day," and "pirates" to the embeddings API, you're getting the vector representation of these concepts as the output. Then, when passing this representation to the message generation API along with the prompt, the model will lock in and use it to generate the response.

Tool use (function calling)

Claude can run actions across external systems via API with its function calling features. But Claude never runs this on the server side: instead, you'll have to configure the tool use on your application. This means that the user (or a system you create) will always have to trigger the external action: Claude will never run it by itself.

Content moderation

If you're dealing with user-generated content, such as a message board, you can use Claude to screen the messages for unsafe content. By passing the keywords or expressions you'd like to screen, you can flag harmful content or any other conversation elements, such as prompt leaks or discussing challenging topics that require moderator attention.

Model output evaluations (evals)

Once you implement a model with your system instructions and users start interacting with it, how do you know whether it's doing the job you want it to do? Enter evaluations (known as evals): a set of parameters you set to find out the quality of the outputs you're getting. 

When you pass an output and a set of evals to Claude, it can judge whether the original AI model did a good job and score it accordingly. If you have multiple API calls with many system prompts, you can understand which ones you should work on first, which are working well, and which ones you should disable and rebuild.

Classification tasks

If you need to find the topics of books, sort 600 unique job titles into eight categories, or get a sentiment analysis of thousands of online reviews, Claude can help. Check out the classification cookbook, and start building.

Browse the prompt library for inspiration

Anthropic dedicates an entire page to collecting useful or interesting prompts to explore implementation options. You can see how to create anything from a website wizard that writes the code for a new site to an ethical dilemma navigator for all your trolley problems.

Brainstorm your use cases with the Workbench

The Claude Workbench

The Workbench helps you test your prompts before implementing them. (Access it after logging in to your Claude developer dashboard and clicking the Workbench link at the top of the screen.) You can change the system and user prompts and see the response on the right side of the screen. Above that response, experiment with the model settings to control how the generation process is handled. 

Anthropic API pricing

The Claude API runs a pay-as-you-go model. Every time you send a request to Anthropic's servers, you'll pay based on the number of tokens in the prompt (input) and the response (output). The pricing for the input is lower than the output, always per million tokens (~750,000 words).

State-of-the-art model

Claude 3.5 Sonnet (best available at time of writing)

  • Input: $3 per million tokens

  • Output: $15 per million tokens

Claude version 3

Claude 3 Haiku (fast and light): 

  • Input: $0.25 per million tokens

  • Output: $1.25 per million tokens

Claude 3.5 Opus (powerful):

  • Input: $15 per million tokens

  • Output: $75 per million tokens

Legacy models

Claude keeps some legacy models still running. You can still call them to see what results you get, even though the pricing is fairly high when compared with Haiku and Sonnet.

Claude 2.0 and 2.1:

  • Input: $8 per million tokens

  • Output: $24 per million tokens

Claude Instant:

  • Input: $0.80 per million tokens

  • Output: $2.40 per million tokens

Managing your Claude billing and credits

When creating a new developer account, you can get $5 free to play around with when you verify your account with a phone number. This credit is consumed every time you use the Workbench—the tool to try out new prompts—as well as any time you run an API call to Anthropic's servers. If you run out of credit, you won't get any responses, so be sure to keep your account topped up.

How to set up Claude API connections

Grab a coffee, turn off distractions, and let's make some magic.

  1. Create an Anthropic account

  2. Add credits to your account

  3. Get your API key

  4. Start a new request

  5. Open the API documentation and reference

  6. Paste the endpoint

  7. Handle authentication

  8. Set up the HTTP method

  9. Set up the request body

  10. Call the AI model

  11. Pass your prompts

  12. Customize the response

  13. Integrate Claude into your apps

Step 1: Create an Anthropic account

First, go to the Anthropic console, and create a developer account.

Step 2: Add credits to your account

As I write this, you can claim $5 in free credits by verifying the account with a phone number. I have a UK-based mobile number, which isn't eligible for this offer, but I imagine most US-based numbers will work fine.

To take advantage of this offer, click the Claim button on the message at the top of the dashboard.

The Claim button in

If you weren't lucky with the free credits offer or just want to add more to test for longer, you'll need to buy them upfront. To do so, click Settings at the top of the screen.

The Settings button on the Claude API platform

Then, click Plans & billing on the left-side menu.

The Plans & billing option in the Claude API settings menu

When signing up for the account, you're placed in the Evaluation plan by default. We need to change that: click the Select Plan button.

The Select Plan in the Claude API Billing section

There are two plans. The best one for building and testing out the API is the Build plan: click Continue to select it. The Scale plan is a better match for bigger businesses that need plenty of requests per minute—if your project evolves and you decide to stay with the Anthropic models, this option could improve user experience.

Choosing a Claude API pricing plan

Anthropic will ask for more information about you and how you plan to use the API. After you fill the form, you'll be prompted to add your billing details. I added $10 to play around with the API.

You can either make this a single-time payment or activate auto-reload: this will automatically fund the account once it drops below a certain level. Last note: taxes are paid separately and will be added at the end of the payment flow. New accounts can only use up to $100 worth of credits every month, but that limit will increase the more you use the API. 

Once you complete these steps, your Plans & billing screen should look something like this:

The Claude API settings page with funds loaded

Step 3: How to get a Claude API key

Still in the Settings tab, click API keys on the left-side menu.

The API keys option in the Claude API menu

Click Create Key.

The Create Key button to create a Claude API key

Name your key, and click Create Key to finish the process.

The Create Key button to create a Claude API key

Copy the key to a safe place—you won't be able to see it again once you close the pop-up.

The success screen after creating a Claude API key

Very important: You need to keep this API key safe at all times. If someone finds your key, they may use it themselves, consuming credits in the process. Don't share this key with anyone who doesn't need it, and if you're publishing an app to the public web, be sure to read up on API security best practices.

Once you close the pop-up, you'll see a list of all the API keys you generated so far. You can disable or delete them by clicking the three-dots icon on the right side.

The list of API keys in the Claude API platform

Step 4: Start a new request

You have credits and an API key: you're ready to call Claude. To help you visualize how this works, I'm going to use Postman—an API design platform—to ask for a text generation task. You can use code, an internal tool builder, or a no-code app builder to get the job done, but if you want to follow along, create a free account in Postman. 

After logging in to the dashboard, on the Send an API Request tab, click the New Request button.

The New Request button in Postman

Step 5: Open the API documentation and reference

Anthropic has rules on how you should make API calls, and you need to follow the structure and instructions to get a result without triggering any errors. There are two important resources you'll need to familiarize yourself with:

  • The API documentation, called User Guides, offers general information about the API, what it can do, and how you can implement it in your projects.

  • The API Reference is a technical reference that helps you set up a call, structure the requests, and see expected responses.

For testing purposes here, I'm going to ask for a text generation job. Here's the API Reference article that discusses this topic. I'll be referring a lot to it, so you can keep it open on a tab on your browser if you're following along.

Step 6: Paste the endpoint

To call the Anthropic API, we need to point Postman to its URL: you can find it on the Authentication page.

Copy the endpoint URL (https://api.anthropic.com/v1/messages).

The endpoint URL from Anthropic

Open Postman, and paste the URL in the input field. The platform will suggest quick setup for Anthropic API, but I suggest you skip it: going the slow way now will help you understand how this works faster.

Pasting the endpoint URL into Postman

Step 7: Handle authentication

The API won't accept calls without knowing who's making them. This is why I generated an API key: I'll have to pass it with my call so Anthropic knows that I'm the one making this call and who to bill the work to.

Each API provider has different rules on how to handle nearly anything about their platform. The best option is always to pay attention to the documentation: in this case, to authenticate myself, I need to pass a value in the header of the call.

The header of the call in the Anthropic API documentation

Let's add this. In Postman, below the endpoint input field in Postman, click Headers.

The Headers option in Postman

"All requests to the Anthropic API must include an x-api-key," so paste x-api key into the key input field.

The x-api-key button in Postman

Then, in front of this key, paste the API key you generated before into the value input field. (You'll notice I've obfuscated the image below—I'm keeping my API key safe—but you'll be able to see everything on your Postman interface.)

Pasting the API key into the Value field in Postman

Step 8: Set up the HTTP method

Open the create message API reference article. Close to the top of the page, you'll see the HTTP method: in this case, it's the POST method. 

Also, notice there's a URL suffix after the POST method: different API features may be served in other URLs, so always be sure you're contacting the right place. We've already pasted the appropriate endpoint in the previous steps, so you don't need to make any changes. But if you're running into errors, be sure to check this out.

The Create a Message documentation from Anthropic API

Back in Postman, click the dropdown displaying the green GET and change it to the yellow POST.

Changing GET to POST in Postman

Learning through mistakes

We've got the endpoint and the authentication ready. Let's make a few mistakes to understand how this works. Click the Send button, and see what happens.

Clicking the Send button in Postman

The response appears at the bottom of the request interface. The Anthropic API returns an HTTP 400 Bad Request error, along with the message that it doesn't accept an empty request body. It's no surprise: we've only set up the endpoint and the authorization header, not the body.

The error message from Postman

Reading errors and understanding them is vital to building successful API calls. Don't get discouraged: always go back to the API documentation to find out what could be wrong, check all the syntax on the request, and use ChatGPT to help if you're completely lost.

Step 9: Set up the request body

Let's go back to the API reference for creating messages. Take a look at the box to the right of the introduction.

The JSON in the Create a Message section of the Anthropic API documentation

This request includes a cURL for people using a command line to interact with the API. We won't need it: we're using Postman. Still, here's what each part means:

curl https://api.anthropic.com/v1/messages \

The curl command tells the system to contact the endpoint URL. The backslash at the end is a line break for cURL, used to improve readability.

--header "x-api-key: $ANTHROPIC_API_KEY" \

--header "anthropic-version: 2023-06-01" \

--header "content-type: application/json" \

The request passes three headers:

  • "x-api-key: followed by your API key handles authentication. We've already configured this in Postman.

  • "anthropic-version: 2023-06-01" states the appropriate system version.

  • "content-type: application/json" sets the content type of the request body so the system knows how to handle it.

Everything after the --data flag is the data of the request, also known as body or payload.

    --data \

'{

    "model": "claude-3-opus-20240229",

    "max_tokens": 1024,

    "messages": [

        {"role": "user", "content": "Hello, world"}

    ]

}'

  • "model": "claude-3-opus-20240229", points to the model you want to call. In this case, it's set to Claude 3 Opus, 29th of February version. You can browse the full model list here and use the names in the column labeled "Latest 1P API model name." I recommend you pick Haiku for your first experiments—it's less expensive.

  • "max_tokens": 1024, sets the maximum output length for each response. In this case, it won't exceed approximately 768 words.

  • "messages": contains the message type along with the content. 

    • "role": "user" sets this message type as a standard user prompt, much like using the Claude chatbot.

    • "content": "Hello, world" is the actual content of the user message.

I'm going to copy the entire request body JSON example without the --data flag or the single quotes at the beginning or end.

Copying the JSON from the documentation

Back in Postman, click Body followed by raw under the endpoint input field. Then, click on line 1 and paste the body there.

Pasting the JSON into Postman

I don't want all my credits to fly away by calling the pricey Claude 3 Opus, so I'm going to change the model value to claude-3-haiku-20240307. Copy and make sure to paste the model within the quotes (and be sure that the model's name syntax is correct).

Changing the model in Postman for the Claude API

Note for readers in the future: The model designation may change over time. If you're getting an error, please refer back to the full model name list, copy the correct name, and paste it in the model parameter.

There's one additional thing we need to take care of: the request needs to pass the Anthropic version in the header. Under the endpoint input field, click on Headers again.

Clicking Headers in Postman

Under the x-api-key header:

  • write anthropic-version in the key input field,

  • and write 2023-06-01 in the value field.

Filling in the anthropic-version information in Postman

Another note for readers in the future: The Anthropic version may have changed since I wrote this. Please refer to the create message API reference, look at the example request on the right side of the screen, and copy/paste the appropriate value into the anthropic-version parameter.

Step 10: Call the AI model

Hey Claude, pick up the phone. Click the Send button, and see what happens.

Clicking the Send button in Postman

If you've followed the guide to the letter so far, you'll get an HTTP 200 OK status code, along with the response from Claude. I've highlighted both in the image below.

The success message and output in Postman

Step 11: Pass your prompts

We're in the game now, but this isn't useful if we can't pass our prompts. I'm going to do two things to customize the response:

  • Set a system message so I can choose Claude's role.

  • Change the user prompt to ask a different question.

I'll start by adding the system message. Scroll down the create message reference page until you can see the system parameter.

The "system" parameter in the Anthropic API documentation

This snippet has important information:

  • system is the parameter's name. We need to pass it in the request body.

  • string means the API expects a sentence with any combination of text, letters, or characters connected to the parameter.

  • The instructions tell you more about how this works. In this case, there's an entire guide to system prompts. (Heads up: the example on that page is structured in Python, not JSON. If you can't guess the correct syntax, paste the example in ChatGPT and ask to regenerate it into JSON.)

Back in Postman, click on Body. Then, under the "max_tokens" parameter, I'm going to add:

"system": "You are an epic dungeon master. As you reply, invite the user to slay a dragon in an adventure.",

You can replace the text in italics here with your system prompt. Be sure to check the syntax: the line must end with a comma after the closing quotes.

Replacing the prompt for the Claude API in Postman

This is just the system message: it controls how Claude behaves, but it isn't a user prompt. I'll change the content of the user message to "What's the best thing to do when it's sunny?"

Changing the prompt for the Claude API

Click Send, and brace for untold adventure.

Clicking Send with the new prompt

If you're following this guide to the letter so far, here's your reward: an HTTP 200 OK and an over-the-top rendition of dungeon master performance.

The success message and output from the Claude API in Postman

Step 12: Customize the response

Just like I added the system key, I can add other key/value pairs to customize the response. You can find instructions on how to do this on the create a message API reference page.

For example, the temperature key adjusts the randomness of the response. Notice the instructions and the accepted value: it only works with a number between 0.0 and 1.0.

The Temperature documentation from Anthropic

If you want to add this to the body, you can do so just below the "max_tokens" key/value pair. Two important notes here: 

  1. Number values don't need quotes around them.

  2. Always finish each line with a comma.

Changing the temperature in Postman

Curious? You can click Send and see how that changes the output.

Step 13: Integrate Claude into your apps

Postman can only test APIs. If you're using a SaaS app that accepts a Claude key, the app will handle all the functionality without setting up calls—you just have to provide the API key.

But if you're using a no-code, low-code, or internal tool builder, you'll need to follow the platform's directions on how to integrate the API. When connecting to external APIs, refer to the documentation of the platform you're using to make the calls. For example, the Bubble no-code builder has an API connector detailing how to replicate the process I've just shown within their platform.

If your work apps don't offer an API connector, or you want to skip the tinkering and move straight to connections, you can instead use Zapier to connect Claude to all the other apps you use. Learn more about how to connect Claude to all your other apps with Zapier, or get started with one of these pre-made workflows.

Write AI-generated email responses with Claude and store in Gmail

Write AI-generated email responses with Claude and store in Gmail
  • Gmail logo
  • Anthropic (Claude) logo
  • Gmail logo
Gmail + Anthropic (Claude)

Create AI-generated posts in WordPress with Claude

Create AI-generated posts in WordPress with Claude
  • Google Sheets logo
  • Anthropic (Claude) logo
  • WordPress logo
Google Sheets + Anthropic (Claude) + WordPress

Generate an AI-analysis of Google Form responses and store in Google Sheets

Generate an AI-analysis of Google Form responses and store in Google Sheets
  • Google Forms logo
  • Anthropic (Claude) logo
  • Google Sheets logo
Google Forms + Anthropic (Claude) + Google Sheets

Send message in Anthropic (Claude) when new transcripts are added in tl;dv

Send message in Anthropic (Claude) when new transcripts are added in tl;dv
  • tl;dv logo
  • Anthropic (Claude) logo
tl;dv + Anthropic (Claude)

Zapier is the leader in workflow automation—integrating with thousands of apps from partners like Google, Salesforce, and Microsoft. Use interfaces, data tables, and logic to build secure, automated systems for your business-critical workflows across your organization's technology stack. Learn more.

Empower your apps with the Claude API

If safety and human feel are key for your project, the Claude model family is the best one. Even if the API lacks the breadth of features of other AI companies, the output quality is unique.

Setting up all the API calls you need can be difficult, especially if you're not used to the process. But don't get discouraged: with time and experimentation, you'll get there. Don't forget to refer back to the API reference and documentation and use ChatGPT to fix your JSON or help you through new calls.

Related reading:

  • Claude vs. ChatGPT: What's the difference?

  • How to write an effective AI prompt

  • The best ChatGPT alternatives

  • How to use the Gemini API

  • How to use the OpenAI API

  • How to use Claude Artifacts

Get productivity tips delivered straight to your inbox

We’ll email you 1-3 times per week—and never share your information.

tags
mentioned apps

Related articles

Improve your productivity automatically. Use Zapier to get your apps working together.

Sign up
See how Zapier works
A Zap with the trigger 'When I get a new lead from Facebook,' and the action 'Notify my team in Slack'