Matt
Matt

Reputation: 73

React Application Requiring Access to 3000+ Entries from Contentful API

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

Answers (2)

Yash Sartanpara
Yash Sartanpara

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

imjared
imjared

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.

https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/limit

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

Related Questions