Search code examples
corscross-domainipfs

How to do cross-origin requests on IPFS


So I want to put a website on ipfs, but it has some javascript which calls out to a server that is not the ipfs gateway, so I get cross origin errors. Any idea how to do this?


Solution

  • You can set the Access-Control-Allow-Origin header and other headers using ipfs config:

    ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
    ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["GET", "POST"]'
    ipfs config --json API.HTTPHeaders.Access-Control-Allow-Headers '["Authorization"]'
    ipfs config --json API.HTTPHeaders.Access-Control-Expose-Headers '["Location"]'
    ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials '["true"]'
    

    The values above are just examples; set the real values to what your client code actually needs.

    https://docs.ipfs.io/reference/api/cli/#ipfs-daemon has the (minimal) existing docs on this.