How to get all Zendesk help articles using their API
Publicado 27 de ago. de 2023
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.
0
1
1 comentário
Entrar para comentar.