Introduction: Using Claude with JavaScript

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!

Recall: Quick Note on JavaScript Setup

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.

Importing and Configuring the Anthropic SDK

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 called ANTHROPIC_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!

Building and Sending a Message to Claude

Let’s break down how to create and send a message to Claude step by step.

Step 1: Create the Message Object

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 a role (like "user" or "assistant") and some content (the text you want to send).
Available models

As of this writing, the current available models are:

Step 2: Send the Message and Wait for a Response

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.
Step 3: Print the Response

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.

Full Example

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.

Summary and What’s Next

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal