High severity HTTP 401 beginner · Fix: 2-5 min

AuthenticationError

replicate.exceptions.AuthenticationError

What this error means
Replicate API client throws AuthenticationError when the REPLICATE_API_TOKEN environment variable is missing or invalid.

Stack trace

traceback
replicate.exceptions.AuthenticationError: Authentication failed: REPLICATE_API_TOKEN missing or invalid
  File "app.py", line 12, in <module>
    client = replicate.Client()
  File "replicate/client.py", line 45, in __init__
    raise AuthenticationError("REPLICATE_API_TOKEN missing")
QUICK FIX
Export your REPLICATE_API_TOKEN environment variable with a valid token before running your script.

Why it happens

The Replicate Python client requires a valid API token set in the REPLICATE_API_TOKEN environment variable for authentication. If this token is missing or empty, the client cannot authenticate requests and raises AuthenticationError.

Detection

Check for AuthenticationError exceptions when initializing the Replicate client or making API calls, and verify that REPLICATE_API_TOKEN is set in the environment before runtime.

Causes & fixes

1

REPLICATE_API_TOKEN environment variable is not set in the system or runtime environment

✓ Fix

Set the REPLICATE_API_TOKEN environment variable with your valid Replicate API token before running your Python application.

2

REPLICATE_API_TOKEN is set but empty or contains only whitespace

✓ Fix

Ensure REPLICATE_API_TOKEN contains the full valid token string without extra spaces or line breaks.

3

Using an outdated Replicate client version that does not support current authentication methods

✓ Fix

Upgrade the replicate package to the latest version using pip install --upgrade replicate.

Code: broken vs fixed

Broken - triggers the error
python
import replicate

client = replicate.Client()  # Raises AuthenticationError if REPLICATE_API_TOKEN missing
print(client.models.list())
Fixed - works correctly
python
import os
import replicate

os.environ["REPLICATE_API_TOKEN"] = "your_valid_token_here"  # Set your token securely
client = replicate.Client()  # Now authenticates successfully
print(client.models.list())
Added setting of REPLICATE_API_TOKEN environment variable before client initialization to provide required authentication token.

Workaround

Manually pass the API token as a parameter to replicate.Client(api_token='your_token') if setting environment variables is not possible immediately.

Prevention

Use environment management tools or secrets managers to securely inject REPLICATE_API_TOKEN into your runtime environment to avoid missing token errors.

Python 3.7+ · replicate >=0.5.0 · tested on 0.6.0
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.