Skip to content

CORS error when using Replicate package in NextJS app #32

@qunash

Description

@qunash

Description

I'm trying to use the replicate package in my NextJS app, but I am getting a CORS error when making API requests.

Code example

import Replicate from "replicate"

const replicate = new Replicate({
  auth: process.env.REPLICATE_API_TOKEN,
})

const predict = async (text: string) => {
  const response = await replicate.run(
    "model_id",
    {
      input: {
        text: text,
      },
    }
  )

  return response
}

predict("example_text")
  .then((result) => console.log(result))
  .catch((error) => console.error(error))

Error from console:

axios.cjs?d475:2337 Refused to set unsafe header "User-Agent"
setRequestHeader @ axios.cjs?d475:2337

Access to XMLHttpRequest at 'https://api.replicate.com/v1/predictions' from origin 'http://localhost:3000' 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.

This error occurs on both localhost and production environments.
Please advise on how to resolve this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions