Problem w/CORS and using redirects to access 3rd party API

Hello,
I have a openai app SpeakAI that is using the serpAPI library w/langchain. I want to incorporate a search agent in my app. The lang/chain library puts in a call to google search via the SerpAPI service but what is happening is serpApi does not allow CORS request so I keep getting error -

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://serpapi.com/search? ......

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://serpapi.com/search?

I read up on redirects on Netlify and setup my toml file as -

# example netlify.toml

[build]

command = "vite build"

functions = "netlify/functions"

publish = "dist"

## Uncomment to use this redirect for Single Page Applications like create-react-app.

## Not needed for static site generators.

[[redirects]]

from = "/search"

to = " https://serpapi.com"

status = 200

force = true

[[redirects]]

from = "/*"

to = "/index.html"

status = 200

Unfortunately I still get the same CORS error. Can anybody offer any advice on what needs to be done here. Thanks in advance.

Not sure how I can check it on your website, but if your frontend is connecting to /search, it shouldn’t have an issue. If you’re still connecting directly to serpapi.com you’d have the issue.

No the request is actually being made by LangChain. I looked in dev tools to see where API call was going to and it is www.serpapi/.com/serach?=xxxxxxx.

Hey @Alan_Killian, welcome!

They way I solved this in my personal projects is by creating a proxy, almost like yours, like this:

[[redirects]]
from = "/search/*"
to = "https://serpapi.com/:splat"
status = 200
force = true

The wildcard /* suffix and the /:splat suffix solved my issue.

Also, I see you have a space before your to = value, I’m not sure if it can cause any problems but I’d remove it. :smiley:

Gr,

Andy

Thanks for reply. Unfortunately still getting CORS error rom serpAPI. I’m oing to try serverless route.

@Alan_Killian, could you let us know how to reproduce this issue?