I need to be able to fetch invoice data from this api: https://apidoc.qoyod.com/
and display it on my page through javascript. First, the user enters an api key and then the page displays the invoice index and details.
This is the code I have so far:
function displayData()
{
const div = document.getElementById("result");
const URL = 'https://www.qoyod.com/api/2.0/invoices/1';
fetch(URL)
.then((resp) => resp.json())
.then(function(data)
{
let invoices = data.results;
invoices.map(function(invoice){
let p = document.createElement('p');
p.innerHTML=`${invoice.index}`;
div.appendChild(p);
})
})
.catch(function(error) {
console.log(json.stringify(error));
})
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Qoyod Assignment</title>
<link href="styles.css" rel="stylesheet">
<script type="text/javascript" src="scripts.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
<div id="root">
<input style="float: left; margin-right: 10px;" type="text" placeholder="Enter API Key" />
<button onClick="displayData();" >Display Data</button>
<div id="result"></div>
</body>
</html>
The challenge here is the "fetch" part. I am sure there is something I am missing out on while extracting the data. Maybe I'm not using the API URL correctly? I just can't seem to be able to "grab" the API data into my"data" object. When I click the button, I get no output at all!
Also, I need to figure out how to use the entered api key to fetch the data. I honestly have no clue at all. It's my first time to work with apis and I feel sooo lost :((
If there are any API pros out there, I would greatly appreciate your assistance!
Thanks
UPDATE: I managed to add the api as a header while fetching the data in this format:
fetch(URL, {
headers: new Headers({
'API-KEY' : '[api-key-here]' })
})
However, I got this error in my browser: "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.qoyod.com/api/2.0/invoices/1. (Reason: CORS request did not succeed)."
Does this mean I need to be granted access by the owner of the api server?