How to get all Zendesk help articles using their API
We have a search function on our website. I need to index all of our Help Desk articles from Zendesk so that they will appear in search results. I currently have the following code, but it will only return the first 30 results.
const https = require('https');
const { Textify } = require('../common/Textify');
const fs = require('fs');
const path = require('path');
function GetZDeskArticles() {
return new Promise((res, rej) => {
const request = https.request(
'https://example.zendesk.com/api/v2/help_center/en-us/articles.json',
(response) => {
let data = '';
response.on('data', (chunck) => {
data = data + chunck.toString();
});
response.on('end', () => {
const body = JSON.parse(data);
res(body.articles);
});
}
);
request.on('error', (error) => {
rej(error);
});
request.end();
});
}
async function StoreZDeskArticles() {
const zdeskArticles = await GetZDeskArticles();
let arts = [];
zdeskArticles.forEach((art) => {
if (art.draft) return;
art.body = Textify(art.body);
arts.push(art);
});
let storagePath = path.resolve(
__dirname,
'../',
'_data',
'ZenDeskArticles.json'
);
fs.writeFileSync(storagePath, JSON.stringify(arts), 'utf8');
}
StoreZDeskArticles();
If I add ?page%5Bsize%5D=100 to the request url, I obviously will get the first 100 results. But I have 164 articles in total that I need to index. I have read through the Zendesk pagination documentation and am unsure of how apply their solution to my situation.
-
I describe it as "offset pagination".
If you received a response for the first page, there will be a "next_page" value in it. You can make a recursive call or use a while statement to fetch until "next_page" is null.
-
Based on "offset pagination", your next_page would be "https://[your doamin].zendesk.com/api/v2/help_center/en-us/articles.json&page=2".
-
Hey,
This is what you're looking for and it explains the concept of paginating through pages to get the full list of results: https://developer.zendesk.com/documentation/api-basics/pagination/paginating-through-lists-using-cursor-pagination/
Hope this helps. :)
Por favor, entrar para comentar.
3 Comentários