getting started

86 views
Skip to first unread message

F Adam

unread,
Nov 27, 2023, 3:18:49 PM11/27/23
to Guardian Open Platform API Forum
HI i have my url set up to fetch the api using vanilla js but doesnt seem to work any ideas?

const apiKey = '*******************'
let params = new URLSearchParams({
'q': 'middle east',
'section': 'technology',
'page-size' : 5,
'orderBy': 'newest',
'api-key': apiKey,
})



const apiUrl = `https://content.guardianapis.com/search/?${params}`;

Guardian Open Platform API Forum

unread,
Nov 28, 2023, 5:52:50 AM11/28/23
to Guardian Open Platform API Forum
Hi,

Are you able to provide us with a bit more information about the problem that you are experiencing? For example, what message or status code do you get back from the Open Platform API when you submit your request?

How are you making the actual HTTP request? In the example that you provided, you are generating a URL (which seems to be correct) but you have not shown the code where you are actually performing an HTTP GET request to this url.

Thanks, Open Platform Team.

F Adam

unread,
Nov 28, 2023, 7:49:44 AM11/28/23
to Guardian Open Platform API Forum
let params = new URLSearchParams({
'q': 'food',
'section': 'news', //news, lifestyle, sports, Opinion Comment is free Columnists Letters Obituaries Inequality Long reads \\


// 'tag': 'healthcare',
'page-size' : 20,
'orderBy': 'newest',
'api-key': apiKey,
'show-fields': 'all'
})



const apiUrl = `https://content.guardianapis.com/search?${params}`;

// console.log(apiUrl)





fetch(apiUrl).then(response => response.json())
.then(data => {
console.log(data)
let container = document.querySelector('.card-container')
let results = data.response.results;
results.forEach(result =>{
let title = document.createElement('h2')
title.innerHTML = result.fields.headline;

let trailText = document.createElement('p')
trailText.innerHTML = result.fields.trailText


let weburl = document.createElement('a')
weburl.setAttribute('href', result.webUrl)
weburl.innerHTML = result.webUrl


let card = document.createElement('div')
card.classList.add('card')


let img = document.createElement('img');
img.setAttribute('src', result.fields.thumbnail)
img.innerHTML = result.fields.thumbnail;

container.appendChild(card);
card.appendChild(img)
card.appendChild(title)
card.appendChild(trailText)
card.appendChild(weburl)

})
})
this seems to be working now. But i dont understand how to use the sections and tags, what are all the possible inputs for those and how would i go about including all possible inputs for section and tags?

Guardian Open Platform API Forum

unread,
Nov 30, 2023, 11:26:28 AM11/30/23
to Guardian Open Platform API Forum
Hi there,

Both sections and tags can be queried from the CAPI.

If you go to https://open-platform.theguardian.com/explore/ and click on the button where it says "Search Content" you can change the form to show you the parameters for searching tags and sections (screenshot attached).
For each of these, when searching, you want to use the contents of the `id` field of the tag/section record as the string to search on.
Screenshot 2023-11-30 at 16.24.27.png

F Adam

unread,
Dec 1, 2023, 9:49:55 AM12/1/23
to Guardian Open Platform API Forum

ok so how would i do that in my hardcoded example , thing i need to make the 'search' dynamic so i can alter it to section or tag?

Guardian Open Platform API Forum

unread,
Dec 4, 2023, 4:37:47 AM12/4/23
to Guardian Open Platform API Forum
Hello,

Our documentation pages explain the URL structure for accessing tags and sections. Have a read of https://open-platform.theguardian.com/documentation/tag and https://open-platform.theguardian.com/documentation/section.

Thanks,
Open Platform Team

Reply all
Reply to author
Forward
0 new messages