Reputation: 1
I just started learning about APIs and I am trying to use the pexels API found here: https://www.pexels.com/api/
I have gotten the API Key, however I am not sure where to put my API key at. I want the result to display JSON.
When I run this code on bash it works, however, I am not sure how to do it inside javascript.
curl -H "Authorization: YOUR_API_KEY" "http://api.pexels.com/v1/search?query=people"
I am running express and request. This is my code.
var express = require("express");
var app = express();
var request = require("request");
app.set("view engine","ejs");
var url = "http://api.pexels.com/v1/search?query=example+query&per_page=15&page=1";
request(url, function(error,response, body){
if(!error && response.statusCode == 200){
console.log(body);
}
});
app.listen(process.env.PORT, process.env.IP, function(){
console.log("server is running!");
});
Any help is greatly appreciated as I am new to this and tried to Google for an answer but couldn't. Thank you!
Upvotes: 0
Views: 3254
Reputation: 11
You need to add header to make api calls,
The code goes this way,
var express = require("express");
var app = express();
var request = require("request");
app.set("view engine","ejs");
var data = {
url : "http://api.pexels.com/v1/search?query=example+query&per_page=15&page=1",
headers: {
'Authorization': 'Your-Api-Key'
}
}
request(data, function(error,response, body){
if(!error && response.statusCode == 200){
console.log(body);
}
});
app.listen(process.env.PORT, process.env.IP, function(){
console.log("server is running!");
});
Upvotes: 1