ajnisbet / opentopodata

Open alternative to the Google Elevation API!
https://www.opentopodata.org
MIT License
312 stars 69 forks source link

How can I call opentopodata from javascript? #62

Closed evgakis closed 2 years ago

evgakis commented 2 years ago

I try to call the api with

`function onGet(url) {

var headers = {}

fetch(url, {
    method: "GET",
    mode: 'no-cors',
    headers: headers
})
    .then((response) => {
        if (!response.ok) {
            throw new Error(response.error)
        }
        return response.json();
    })
    .then(data => {
        dat = data.messages;
    })
    .catch(function (error) {
        err = error.toString();
        var test = 1;
        ee = test;
    });

}`

but I receive an error. Is it possible to call https://api.opentopodata.org/v1/eudem25m?locations= from javascript?

ajnisbet commented 2 years ago

Hi, thanks for opening an issue!

If you host opentopodata yourself you can call it from the browser just fine, the software supports it.

But for the public api that I host at api.opentopodata.org, CORS requests are unfortunately disabled as the bypass the IP-based ratelimiting I use to ensure the server is stable for everyone.

There's more information here: https://github.com/ajnisbet/opentopodata/issues/9

The open software in this repo has full support for browser-based fetch requests and CORS policy. However, the free instance I'm hosting at api.opentopodata.org is configured to block cross-origin requests. These requests effectively bypass the per-IP rate limit, and I want to make sure the server has enough capacity for everyone!

So there are a couple of options to get this working:

  • Host ajnisbet/opentopodata on your own server with CORS enabled
  • Proxy requests from api.opentopodata.org through your own server, adding the appropriate CORS header
  • If you're working on academic research or a small non-commercial project, send me an email and I can add you to a whitelist on the public api.
  • Use the GPXZ elevation api