Cloudflare Docs
Workers AI
Edit this page on GitHub
Set theme to dark (⇧+D)

Get started with the Workers AI REST API

This guide will instruct you through setting up and deploying your first Workers AI project. You will use the Workers AI REST API to experiment with a large language model (LLM).

​​ Prerequisites

Sign up for a Cloudflare account if you have not already.

​​ 1. Get an API token

To create an API token:

  1. Log in to the Cloudflare dashboard.
  2. Select the user icon on the top right of your dashboard > My Profile.
  3. Select API Tokens > Create Token.
  4. Go to the Workers AI template > select Use template. You can also go to Create Custom Token > Get started to customize a token.
  5. Select Continue to summary > in the Workers AI API token summary, select Create Token.

​​ 2. Run a model via API

After creating your API token, authenticate and make requests to the API using your API token in the request.

You will use the Execute AI model endpoint to run the @cloudflare/meta-llama/llama-2-7b-chat-int8 model:

$ curl https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-2-7b-chat-int8 \
-H 'Authorization: Bearer {API_TOKEN}' \
-d '{ "prompt": "Where did the phrase Hello World come from" }'

Replace {ACCOUNT_ID} with your account ID and {API_token} with the API token you generated in step 1.

The API response will look like the following:

{
"result": {
"response": "Hello, World first appeared in 1974 at Bell Labs when Brian Kernighan included it in the C programming language example. It became widely used as a basic test program due to simplicity and clarity. It represents an inviting greeting from a program to the world."
},
"success": true,
"errors": [],
"messages": []
}

This example execution uses the @cf/meta/llama-2-7b-chat-int8 model, but you can use any of the models in the Workers AI models catalog. If using another model, you will need to replace {model} with your desired model name.

By completing this guide, you have created a Cloudflare account (if you did not have one already) and an API token that grants Workers AI read permissions to your account. You executed the @cloudflare/meta-llama/llama-2-7b-chat-int8 model using a cURL command from the terminal and received an answer to your prompt in a JSON response.

  • Models - Browse the Workers AI models catalog.