1
I have an async Function that does a get for an api and returns a random word:
word js.
require('dotenv').config();
const config = require('./config.js')
const axios = require('axios');
const axiosConfig = {
headers: { // speciyfy the headers
'X-Mashape-Key': config.wdKey,
"Accept": "application/json"
}
}
module.exports = async function requestWord() {
const response = await axios.get('https://wordsapiv1.p.mashape.com/words/?random=true', axiosConfig)
return await response.data.word
}
Ai I want to use the return of this function, in case the word, as parameter in another function that makes the get in the google api.
image js.
require('dotenv').config();
const requestWord = require('./words.js')
const {google} = require('googleapis')
const config = require('./config.js')
const customSearch = google.customsearch('v1')
const f = requestWord
console.log(f())
async function searchImages(word){
try{
let URLs
const URLsArray = await fetchAndReturnURLs(word)
async function fetchAndReturnURLs(query){
const response = await customSearch.cse.list({
auth: config.ggApiKey,
cx: config.ggCx,
q: query,
searchType: 'image',
num: 2,
})
URLs = await response.data.items.map((item) => {
return item.link
})
}
return URLs
}
catch(err){
}
}
this console.log(f()) returns Promise { <pending> }
This address you are trying to access is asking for the API key to authenticate your request: API Keys. In the
headers
you will have to add a line with the API key"X-RapidAPI-Key": {Chave-da-API}
– Augusto Vasques