The fastest way to use the Claude API in Python is to install anthropic, set your API key, and call client.messages.create(). You’ll have a working response in under a minute:

Claude is Anthropic’s large language model, accessible via a clean REST API with an official Python SDK. Unlike heavier AI frameworks that require you to wire up multiple components before you see any output, the anthropic package gets you to a working response in a handful of lines.
In the following steps, you’ll install the anthropic SDK, call Claude from Python, shape Claude’s behavior with a system prompt, and then return structured JSON output using a schema or Pydantic.
Note: Claude’s responses are non-deterministic, so the same prompt produces different output each time, which is expected for a large language model. Also, API calls cost money based on the number of tokens processed. Keep an eye on your usage in the Claude Console as you follow along.
Each step builds on the last, and the final script is short enough to read in one sitting but complete enough to extend into a real application of your own.
Get Your Code: Click here to download the free sample code that shows you how to use the Claude API in Python.
Take the Quiz: Test your knowledge with our interactive “How to Use the Claude API in Python” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
How to Use the Claude API in PythonTest your understanding of using the Claude API in Python. Send prompts, set system instructions, and return structured JSON with a schema.
Prerequisites
Before diving in, make sure you have the following in place:
-
Python knowledge: You should be comfortable with Python basics, like defining functions, running scripts from the terminal, and working with virtual environments. If virtual environments are new to you, Python Virtual Environments: A Primer has you covered before you continue.
-
Python 3.9 or higher: The
anthropicSDK requires Python 3.9 as a minimum. If you’re not sure which version you have, runpython --versionin your terminal. If you need to install or upgrade, follow the steps in the guide on installing Python. -
An Anthropic account: You’ll need an Anthropic account to generate an API key in the Claude Console. Step 1 will show you how to find and secure your key once you’re in.
Don’t worry if you’ve never worked with an API before. This tutorial will walk you through authentication and help you make your first request from scratch.
Step 1: Set Up the Claude API in Python
Before you can call Claude from Python, you need an API key and the anthropic package installed. By the end of this step, you’ll have both, and Claude will be responding to your first prompt.
Get Your API Key and Install anthropic
Log in to the Claude Console or create a new account. If you’re starting fresh, you can begin using the API after adding $5 of credits.
Then navigate to the API Keys section. Click Create Key, give it a descriptive name like real-python-tutorial, and copy it immediately. You won’t see it again after you close the dialog.
Note: Never paste your API key directly into your code. Instead, store it as an environment variable. The anthropic SDK automatically reads it from ANTHROPIC_API_KEY at runtime, so you never need to reference it explicitly in your scripts.
Storing your key as an environment variable means it never touches your source code or version control history. The exact command depends on your operating system:
With your API key stored safely, you’re ready to install the SDK. Create a fresh virtual environment and activate it before installing anything. This isolation prevents the anthropic package from conflicting with your system-level tools.



