Initial setup
Let’s learn how to use the Anthropic API to build with Claude.
In this example, we’ll have Claude write a Python function that checks if a string is a palindrome.
Prerequisites
You will need:
- An Anthropic Console account
- An API key
- Python 3.7+ or TypeScript 4.5+
Anthropic provides Python and TypeScript SDKs, although you can make direct HTTP requests to the API.
Start with the Workbench
Any API call you make–-regardless of the specific task-–sends a well-configured prompt to the Anthropic API. As you’re learning to make the most of Claude, we recommend that you start the development process in the Workbench, a web-based interface to Claude.
Log into the Anthropic Console and click Workbench.
In the middle section, under User, let’s ask Claude a question.
Click Run. On the right side, you’ll see output like
This is a good answer, but let’s say we wanted to control the exact type of answer Claude gives. For example, only allowing Claude to respond to questions with poems. We can control the format, tone, and personality of the response by adding a System Prompt.
Click Run again.
See how Claude’s response has changed? LLMs respond well to clear and direct instructions. You can put the role instructions in either the system prompt or the user message. We recommend testing to see which way yields the best results for your use case.
Once you’ve tweaked the inputs such that you’re pleased with the output–-and have a good sense how to use Claude–-convert your Workbench into an integration.
Install the SDK
Anthropic provides SDKs for Python (3.7+) and TypeScript (4.5+).
In your project directory, create a virtual environment.
Activate the virtual environment using
- On macOS or Linux,
source claude-env/bin/activate
- On Windows,
claude-env\Scripts\activate
Set your API key
Every API call requires a valid API key. The SDKs are designed to pull the API key from an environmental variable ANTHROPIC_API_KEY
. You can also supply the key to the Anthropic client when initializing it.
Call the API
Call the API by passing the proper parameters to the /messages/create endpoint.
Note that the code provided by the Workbench sets the API key in the constructor. If you set the API key as an environment variable, you can omit that line as below.
Run the code using python3 claude_quickstart.py
or node claude_quickstart.js
.
This quickstart shows how to develop a basic, but functional, Claude-powered application using the Console, Workbench, and API. You can use this same workflow as the foundation for much more powerful use cases.
Next steps
Now that you have made your first Anthropic API request, it’s time to explore what else is possible: