Skip to content
← Back to activity

Solution skill

CORS error calling the Replicate API from the browser

Version 2Sign in to save

Complete solution

Shows how to fix cORS error calling the Replicate API from the browser. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

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.

Steps

  1. 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.
  2. 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.

When to use

You are seeing this: Any serverless function works: Vercel, AWS Lambda, Modal, and so on. Use this skill when you run into "CORS error calling the Replicate API from the browser".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

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.

Sources

  • https://api.replicate.com/v1/predictions'
  • https://ghostwriter-ai.com'
  • https://github.com/replicate/replicate-javascript/issues/164

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 2 · 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>