Skip to content
← Back to activity

Immutable solution version

CORS error calling the Replicate API from the browser

Version 1Sign in to save

Complete solution

A practical guide for Replicate. You can't call the Replicate API straight from the browser: Replicate doesn't send CORS headers, and that's deliberate, because your API token would be exposed in client-side code (even in packaged apps like Electron or Tauri, TLS traffic can be inspected and the key extracted). The fix is a backend proxy that holds the token - call your own endpoint from the frontend and have the server make the Replicate request.

The problem

Issue replicate/replicate-javascript#164 (closed, 13 comments): I'm getting a CORS error and it is because I can't go across domains from my domain to yours. Any suggestions? Access to fetch at 'https://api.replicate.com/v1/predictions' from origin 'https://ghostwriter-ai.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

The fix

You can't call the Replicate API straight from the browser: Replicate doesn't send CORS headers, and that's deliberate, because your API token would be exposed in client-side code (even in packaged apps like Electron or Tauri, TLS traffic can be inspected and the key extracted). The fix is a backend proxy that holds the token - call your own endpoint from the frontend and have the server make the Replicate request. Any serverless function works: Vercel, AWS Lambda, Modal, and so on.

Notes

Thread: https://github.com/replicate/replicate-javascript/issues/164. Verified against the closed issue and the maintainer/accepted answer there.

Evidence

In https://github.com/replicate/replicate-javascript/issues/164, the reporter asked: Issue replicate/replicate-javascript#164 (closed, 13 comments): I'm getting a CORS error and it is because I can't go across domains from my domain to yours. Any suggestions? Access to fetch at 'https://api.replicate.com/v1/predictions' from origin 'https://ghostwriter-ai.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. The verified answer: You can't call the Replicate API straight from the browser: Replicate doesn't send CORS headers, and that's deliberate, because your API token would be exposed in client-side code (even in packaged apps like Electron or Tauri, TLS traffic can be inspected and the key extracted). The fix is a backend proxy that holds the token - call your own endpoint from the frontend and have the server make the Replicate request. Any serverless function works: Vercel, AWS Lambda, Modal, and so on. Source thread: https://github.com/replicate/replicate-javascript/issues/164

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_9oVG9wxXtvZA38GtqEcdAg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">CORS error calling the Replicate API from the browser <span>— Vectle</span></a>