Reputation: 73
I am trying to access more than the default 1000 entries at once. How do I get the full 3,091 entries that I need immediately?
I can see in the console when I log 'response.data' from the API request that 'limit' is set to 1000. But I don't know how to increase that.
GET Request:
axios.get(`https://cdn.contentful.com/spaces/${keys.space}/entries?access_token=${keys.accessToken}&limit=1000`)
Console log shown here:
items: (1000) [{…}, {…}, {…}, {…},...]
limit: 1000
skip: 0
sys: {type: "Array"}
total: 3091
Upvotes: 2
Views: 632
Reputation: 86
You can use contentful's limit and skip parameters.
Below is the function to get all the entries. It will loop till the Total number of entries.
const getContentfulData = async ({ allentries = [], skip = 0 } = {}) => {
const results = await axios.get(`https://cdn.contentful.com/spaces/${keys.space}/entries?access_token=${keys.accessToken}&limit=1000&skip=${skip}`);
const entries = [...allentries, ...results.items];
return entries.length < results.total ?
getContentfulData({ allentries: entries, skip: skip + 1000 })
: entries
};
Upvotes: 1
Reputation: 20554
The contentful docs explicitly say you can request a maximum of 1000 entries at a time:
Note: The maximum number of entries returned by the API is 1000. The API will throw a BadRequestError for values higher than 1000 and values other than an integer. The default number of entries returned by the API is 100.
You'll need to leverage their skip
parameter and make 4 requests.
page 1: skip=0, limit=1000 (entries 0-1000)
page 2: skip=1000, limit=1000 (entries 1000-2000)
page 3: skip=2000, limit=1000 (entries 2000-3000)
page 4: skip=3000, limit=1000 (entries 3000-...)
Upvotes: 4