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 API token and Account ID

You need your API token and Account ID to use the REST API.

To get these values:

  1. Log in to the Cloudflare dashboard and select your account.
  2. Go to AI > Workers AI.
  3. Select Using the Workers AI API.
  4. Get your API token:
    1. Select Get API Token.
    2. Review the prefilled information.
    3. Select Continue to summary.
    4. Select Create Token.
    5. Copy the token value for future steps.
  5. Get your account ID:
    1. Go back to the Using the Workers AI API screen (your original tab).
    2. For Get Account ID, copy the value for Account ID.

​​ 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 the values for {ACCOUNT_ID} and {API_token}.

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.