Retrieve all items from a library

60 views
Skip to first unread message

Lucas Teixeira

unread,
Oct 19, 2021, 12:03:48 PM10/19/21
to zotero-dev
I've been trying to make a simple request which takes all the items from a library. The library that I'm querying has at least about 1-2 thousand titles, but when I query the library I get a response that's only 25 items long The library I've been querying is:


This is a minimum working example in JS of my query:

const request = require('request');
request('https://api.zotero.org/groups/2420932/items?v=3', async (error, response, body) => {
  console.error('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status
  const raw = await JSON.parse(body)
  console.log('number of items:', raw.length);
}

Tomasz Najdek

unread,
Oct 19, 2021, 12:20:01 PM10/19/21
to zoter...@googlegroups.com
This is simply the first page of results, you can request a second one by including `start=25` in your query params and you can control the number of items returned with `limit=25` (with a 100 being maximum supported). Here is example based on your own code that will get next 50 items.

request('https://api.zotero.org/groups/2420932/items?start=25&limit=50', handler);

The response actually includes a URL for the next page in a header. This is all documented here:


‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
--
You received this message because you are subscribed to the Google Groups "zotero-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zotero-dev+...@googlegroups.com.

Lucas Teixeira

unread,
Oct 19, 2021, 11:41:19 PM10/19/21
to zotero-dev
Ahh, gotcha. Thank you so much for the quick reply!

Another question:

The group page says that this library only has 1760 items

And yet the API will continue to return 100 items well beyond start=1760, it's not until I set start=5200 that I get a response of length 36. Seeming to suggest that the library actually has 5236 items

Why is this the case?

Dan Stillman

unread,
Oct 19, 2021, 11:43:22 PM10/19/21
to zoter...@googlegroups.com
On 10/19/21 4:49 PM, Lucas Teixeira wrote:
> The group page says that this library only has 1760 items
> https://www.zotero.org/groups/2420932/tai_safety_bibliography/
>
> And yet the API will continue to return 100 items well beyond
> start=1760, it's not until I set start=5200 that I get a response of
> length 36. Seeming to suggest that the library actually has 5236 items

Top-level items (/items/top) vs. all items (/items)

valtermedeiros.com

unread,
Oct 26, 2021, 9:46:51 PM10/26/21
to zotero-dev
I'm creating my own "page" filter from this query. for example, given a "/items?page=2" I grab the number 2 and do the calculation of items given a "limit" and a "start", so what I do is   "/items?page=2" ---> becomes ---> "/items?start=50&limit=50"  (2 * itemsPerPage)

So an example:

For "page = 0"

start = page(0) X itemsPerPage(50) 
= 0 offset

Lucas Teixeira

unread,
Oct 27, 2021, 6:39:48 PM10/27/21
to zotero-dev
Oh thanks, that makes sense. Do queries to items/top return all the items which don't have a parent element?

Lucas Teixeira

unread,
Oct 27, 2021, 6:46:46 PM10/27/21
to zotero-dev
This is the JS that I ended up writing to deal with this.

// Makes request to API and filters out request metadata
const zoteroGroupsRequest = (groupId) => async (index) => {
const res = await fetch(`https://api.zotero.org/groups/${groupId}/items?v=3&start=${index * 100}&limit=100`).catch(err => console.log(err));
const preData = await res.json();
const data = preData.map(x => x.data);
return data
}

// Recursive shell around zoteroGroupsRequest which queries all items of a group's library
const recursiveShell = (groupId) => async (index = 0, result = []) => {
const data = await zoteroGroupsRequest(groupId)(index)
if (data.length < 100) return result.concat(data)
else return shell(index + 1, result.concat(data))
}
Reply all
Reply to author
Forward
0 new messages