Skip to main content

Examples of Statistical API in javascript with axios


Hi all,

This is more a javascript question than a Sentinel Hub question but I hope that some people here know the answer.

I saw the announcement of the new statistical API and its documentation at:

docs.sentinel-hub.com
d3714e73b38a87afa3c31502a6696052a7395163.png

Examples of Statistical API

Statistical API examples in Python and a Postman collection. To execute them you need to create an OAuth client.

However I could not wrap my head around that axios/oauth business. I am able to use axios to get an access token to use the Catalog API, but I do not know how to use this access token as the “oauth” object shown in the previous link to make POST requests against the new statistical API URL.

Has anyone managed to make work the whole pipeline?

PS: the axios documentation was of no help either.

3 replies

Hi.

If you managed to get an authorization token you can use it by passing it as an Authentication header like this:


axios.post("https://services.sentinel-hub.com/api/v1/statistics", data, { headers: { 'Authorization': `Bearer ${your_token_here}`}});

Where data is the JSON object that you are sending to the API (I suggest using Statistical mode on Requests Builder, under Request Preview to get that object in a more friendly way).

To make auth work, basically you need to attach the HTTP header Authorization: Bearer <token> to your request.

Let me know if I can assist you further.

 

Edit: Note that you may also need to add ‘Content-type’: ‘application/json’ and ‘Accept’: ‘application/json’ headers in the headers object passed to axios.


Thanks! I can now make requests to the statistical API. I hace more questions but I will open another thread for them.


 but this approach has a problem with token , every time the token expires you have to change it in code which not a good pratcice , is there any other alternative


Reply