2

I'm currently working on a project where I use the 0x API on the Goerli network to swap tokens. I'm making an HTTP GET request using axios from a web application served from http://localhost:3000.

Here is a snippet of my code:

const host = 'https://goerli.api.0x.org';
const headers = {
  '0x-api-key': '[api-key]', 
};
const response = await axios
  .get(`${host}/swap/v1/quote?sellToken=ETH&buyToken=DAI&sellAmount=10000000`, {
    headers,
  })
  .catch(error => {
    throw new Error(`Failed to get quote: ${error.message}`);
  });

When I try to execute this request, I encounter a CORS error stating that my 0x-api-key header is not allowed:

Access to XMLHttpRequest at 'https://goerli.api.0x.org/swap/v1/quote?sellToken=ETH&buyToken=DAI&sellAmount=10000000' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field 0x-api-key is not allowed by Access-Control-Allow-Headers in preflight response.

Given the error message, it seems the server's CORS policy does not allow the 0x-api-key header field. I've tried several methods to solve this, but none of them worked.

I have confirmed that the same request works on Mainnet. It seems to be an issue specific to Goerli.

I have also tried a variety of different token pairs, pairs that I know to have liquidity.

I understand that CORS is a browser-enforced policy to ensure the security of web applications, but I'm confused about why the 0x-api-key header is not allowed. Is this a setting on your server's end, or is there a way I can adjust my request to align with the server's CORS policy? Any guidance on how to address this issue would be greatly appreciated.

Thanks in advance for your help!

1 Answer 1

1

I found the solution for any who stumble across this. It turns out it happens when you include the API key when using Goerli. All I had to do was remove the API key.

The 0x documentation should really be updated on how to use testnet properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.