Failed API Call - How to handle "Error 400" for Jurassic API?

128 views Asked by At

When setting up the API call for Jurassic (requested from https://studio.ai21.com/docs/api/), I always get the 400 error response.

import json
import requests

   requests.post(
    "https://api.ai21.com/studio/v1/j1-large/complete",
    headers={"Authorization": "PERSONAL API KEY"}, 
    json={
        "prompt": "Life is like", 
        "numResults": 1, 
        "maxTokens": 8, 
        "stopSequences": ["."],
        "topKReturn": 0,
        "temperature": 0.0
    }
)

Output: <Response [400]>

Could anyone give me some advice please?

1

There are 1 answers

0
RJ Adriaansen On

Your headers are wrong, as the documentation indicates that the API key should be preceded by Bearer . Try this:

import json
import requests

YOUR_API_KEY = 12345678 #define API key here

requests.post(
    "https://api.ai21.com/studio/v1/j1-large/complete",
    headers={"Authorization": f"Bearer {YOUR_API_KEY}"},
    json={
        "prompt": "Life is like", 
        "numResults": 1, 
        "maxTokens": 8, 
        "stopSequences": ["."],
        "topKReturn": 0,
        "temperature": 0.0
    }
)