How to Access Claude 3 API for Opus and Sonnet Models (With Examples)

In Short
  • Anthropic is offering $5 worth of free API access to users and developers.
  • You can start using the API for Opus and Sonnet models. However, API access for the smallest Haiku model is not available yet.
  • You can also try some code examples and test the models on your prompt.

Anthropic introduced a new family of Claude 3 models which include Opus (largest), Sonnet (mid-size), and Haiku (smallest). The company has also made the API for Claude 3 models available immediately.

While Claude 3 API pricing, especially for the Opus model, is quite expensive in comparison to GPT-4 Turbo, users and developers want to check out the model’s capabilities. So here is a simple tutorial on how to access Claude 3 API for Opus and Sonnet models. We have also added a few code examples for you to start testing the models.

Note: Anthropic is currently offering Claude 3 API worth $5 in free credits. Before you buy the API, you can claim free credits and test Opus and Sonnet models right away.

Access Claude 3 API Key For Free

  • Head to (visit) and create an account.
  • Next, you will see a banner on top informing users about Claude offering $5 in free credits. Click on “Claim“.
claim free credit by Anthropic
  • Enter your mobile number and verify it. You are almost done.
  • Now, click on “Get API Keys” on the dashboard. You can also head to (visit) to get Claude 3 API keys.
get claude 3 API key
  • Here, click on “Create Key” and give it a name.
name your API key
  • Now, copy the API key and store it securely.
copy the API key

Tutorial to Use Claude 3 API With Examples

  • Go ahead and set up Python along with Pip on your computer.
  • After that, open Terminal and run the below command to install the Claude library.
pip install anthropic
install claude library via pip
  • Anthropic has added some great examples in its documentation for Claude 3 API testing. You can copy the below code and paste it into a code editor like Notepad++.
import anthropic

client = anthropic.Anthropic(
    # defaults to os.environ.get("ANTHROPIC_API_KEY")

message = client.messages.create(
    system="Respond only in Yoda-speak.",
        {"role": "user", "content": "How are you today?"}

  • The code is already using the largest Claude 3 Opus model (claude-3-opus-20240229). You just need to replace my_api_key with the actual API key you copied above. If you want to use the Sonnet model, use this model name: claude-3-sonnet-20240229.
code to demonstrate claude 3 opus API
  • Now, save it as on your Desktop or your preferred location. You can also name it differently, but make sure .py is appended.
  • Finally, fire up Terminal, and move to the Desktop. Next, run the file. It will answer the question set in the code. Here, it replies like Yoda to the question, “How are you today?” You can also change the system prompt in the code to change its behavior.
cd Desktop
run code using claude 3 api
  • Apart from that, you can try out Simon Willison’s newly released plugin for Claude 3 models with ease.

So that is how you can quickly access the Claude 3 API and test both Opus and Sonnet models. Anthropic is currently not offering API for the smallest Haiku model. If that changes in the future, we will let you know. Anyway, that is all from us. If you are looking to access the Gemini API key, follow our linked guide. And if you have any questions, let us know in the comment section below.

comment Comments 0
Leave a Reply