Welcome back! In the last lesson, you learned how to send your first message to Claude using Python. Now, we will do something very similar, but this time using JavaScript and the Anthropic SDK. This is helpful if you want to build web applications or work in environments where JavaScript is the main language.
By the end of this lesson, you will know how to:
- Import the
Anthropic
SDK in JavaScript - Set up your API key
- Build and send a message to Claude
- Print out Claude’s response
Let’s get started!
Before we dive in, here’s a quick reminder about setting up your environment:
- On CodeSignal, the Anthropic SDK for JavaScript (
@anthropic-ai/sdk
) is already installed for you. You do not need to install anything extra to follow along in this course. - If you want to try this on your own computer, you can install the SDK using
npm
: - You will also need an Anthropic API key. On CodeSignal, you do not need to worry about this, but on your own device, you would set it as an environment variable or pass it directly in your code.
Now, let’s move on to the main steps.
The first step is to import the Anthropic
SDK into your JavaScript file and set up the configuration.
Start by importing the SDK at the top of your file:
This line tells JavaScript to use the Anthropic SDK, which gives you access to all the tools you need to talk to Claude.
Next, you need to create an instance of the Anthropic
client. This is how your code will connect to the Claude API.
- Here,
anthropic
is your client object. - If you are running this on your own computer, you can provide your API key directly by replacing
'my_api_key'
with your actual key, or you can set it as an environment variable calledANTHROPIC_API_KEY
. - On CodeSignal, you do not need to set the API key; it is handled for you.
Now you are ready to send a message!
Let’s break down how to create and send a message to Claude step by step.
You need to tell Claude who is speaking and what you want to say. This is done by creating a message object.
model
specifies which version of Claude you want to use. Here, we use"claude-sonnet-4-20250514"
.max_tokens
controls how long the response can be. A token is a piece of text, like a word or part of a word.messages
is an array (a list) of message objects. Each message has arole
(like"user"
or"assistant"
) and somecontent
(the text you want to send).
As of this writing, the current available models are:
Now, use the SDK to send your message and wait for Claude’s reply.
anthropic.messages.create(message)
sends your message to Claude.- The
await
keyword tells JavaScript to wait for Claude’s response before moving on. This is important because talking to an API can take a little time.
Finally, you can print out the response you get from Claude.
This will show you the full response object from Claude, which includes the text Claude generated and some extra information.
Here is the complete code, putting all the steps together:
Expected Output:
When you run this code, you will see an object printed to the console. It will look something like this (the actual content may vary):
The important part is the content
field, which contains Claude’s reply.
In this lesson, you learned how to:
- Import and configure the
Anthropic
SDK in JavaScript - Build a message object to send to Claude
- Send the message and print out the response
You are now ready to practice these steps on your own. In the next exercises, you will get hands-on experience sending different messages to Claude and exploring the responses. Good luck, and have fun experimenting!
