Authentication

📘

Note

The Twelve Labs Video Understanding API is currently in closed beta. To create an account, you must first sign up for a demo. To do so, go to the Request a Demo page and follow the instructions. You will receive a signup code that you can use to create an account.

The API uses keys for authentication, and your key is similar to a password. Avoid distributing your API key to other users, or hard-coding it. Generate a new key whenever you think your account may have been compromised.

For security reasons, your API key automatically expires every two months. When your key has expired, you must generate a new one to continue using the API.

📘

Notes

  • If your policies require you to rotate keys more often, you can always generate a new key.
  • If you want to pause using the API without deleting your account, you can revoke your API key.

The process of managing your API key can be divided into the following tasks:

  • Retrieve your API key: Go to the Dashboard page, and select the Copy button under the API Key section.
  • Generate a new API key: Go to the Dashboard page, choose Settings from the sidebar, and then select the Regenerate Key button under the API Key section.
  • Revoke an API key: Go to the Dashboard page, choose Settings from the sidebar, and then select the Revoke Key button under the API Key section.

Making requests to the API

The example code in this section assumes the following:

  • Your API key is stored in a variable named API_KEY.
  • The URL of the API service is stored in a variable named API_URL.

The example creates a new index by calling the POST method of the /indexes endpoint. Note that, every time you make a request, you must pass the x-api-key header parameter with the value of your API key:

# Declare the `headers` dictionary containing your API key
headers = {
    "x-api-key": API_KEY
}

INDEXES_URL = f"{API_URL}/indexes"
INDEX_NAME = "<YOUR_INDEX_NAME>" 
data = {
    "engine_id": "marengo2",
    "index_options": ["visual", "conversation", "text_in_video"],
    "index_name": INDEX_NAME,
}
# Make a POST request to the `indexes` endpoint, passing the `x-api-key` header parameter
response = requests.post(INDEXES_URL, headers=headers, json=data)
INDEX_ID = response.json().get('_id')
print (f'Status code: {response.status_code}')
pprint (response.json())
// Declare the `headers` object containing your API key
const headers = {
  'x-api-key': API_KEY
}
 
const INDEXES_URL = `${API_URL}/indexes`
const INDEX_NAME = '<YOUR_INDEX_NAME>' 

// Make a POST request to the `indexes` endpoint, passing the `x-api-key` header parameter
const { data: response } = await axios.post(
    INDEXES_URL,
    {
    "engine_id": "marengo",
    "index_options": ["visual", "conversation", "text_in_video"],
    "index_name": INDEX_NAME
  },
  { headers }
)
const INDEX_ID = response._id
console.log(`Status code: ${resp.status}`)
console.log(response)

If your key has expired or is invalid, the response should look similar to the following:

{
    "code": 401,
    "error": "token is not valid",
    "message": "Please provide a valid token"
}