API - listDocs() limit?

Im using the Coda API to try to create myself a docs doc. Just a simple doc with a table that contains one row per document that I have access to within any workspace.

Two questions for you API geniuses out there:

Question 1
Whenever I run my code, it is only returning 100 documents flat. Is this a preset limit? Can Coda.api.listDocs() only return 100 documents? Or is there something I am missing?

Question 2
The documentation states that the listDocs() takes a query parameter of workspaceId to only return documents within a specific workspace. How do I find my workspace ID? Is it in the URL itself when on my Coda.io homepage?

Thanks! (If it helps, Im using Google Apps Script)

1 Like

Hey @Scott_Collier-Weir! Happy to help you out with these questions.

Question 1
Yes, there’s a limit of 100 docs by default (configurable using the limit parameter, but looks like it’s capped at 100 anyway). Generally list APIs return a finite list of items so that individual requests don’t get too slow. If you’d like to retrieve the next page of results, you’ll need to take the nextPageToken from the results of the first listApiDocs() call and pass that into the pageToken parameter when you call listDocs(). You’ll likely want to use a loop to keep calling that API until you reach the desired number of docs or there is no more nextPageToken.

The API has documentation on list endpoints you can look at; Google Apps Script basically has the same parameters. if you search for listDocs in the Apps Script library you’ll see some documentation there too.

Question 2
There are several ways of discovering the workspaceId. The easiest way is to click on your workspace in the doc list and look at the URL bar, as you mentioned. It will look like ws-abcd1234. We’ll update our API docs to mention this. Another way of getting it is to look at the listDocs response - you should see the workspaceId there as well.

1 Like

Thank you thank you @oleg !
Much for me to digest here and one more question if you’d be so kind.

I guessed the workspaceId and attempted to add it as a parameter to my listDocs() call like so:
Coda.api.listDocs(workspaceId=“my workspace is”)

But it still returned docs outside the target workspace. Did I write it incorrectly??

Google Apps Script syntax is very similar to JavaScript’s, so you’ll want to write something like:

CodaAPI.listDocs({workspaceId: 'ws-abcd1234'});

If you haven’t seen it, I suggest going through the Getting Started Guide to get a hang for the syntax, and you can look through the library source to see the parameters you’ll need to specify.

Ok sorry to bug you again - I got everything in my API call working to create a docsDoc except for the whole “Pass back the nextPageToken” thing.

Two main issues:

Issue 1
When I console.log() my listDocs() call its not including the token at all at the end of it, but it is logging that the output is too large and therefore its truncated.

On top of that, when I console.log() a listRows() call on some of my tables it does include the nextPageToken. Any idea whats going on?

Issue 2
In your post above mentioned that:

I know how to pass info into a parameter now (thanks!) but do I have to make a second listDocs() call with the pageToken parameter, or pass the pageToken parameter into the initial listDocs() call?

Thanks! Im so close to finishing this thing so appreciate any help I can get!

1 Like

Hi @Scott_Collier-Weir - Here’s some sample code that should make the pattern more clear:

const API_TOKEN = "...";
const PAGE_SIZE = 100;

CodaAPI.authenticate(API_TOKEN);

function countMyDocs() {
  let count = 0;
  let pageToken = "";
  do {
    console.log("Making request.")
    let response = CodaAPI.listDocs({
      pageToken: pageToken,
      limit: PAGE_SIZE,
    });
    let docs = response.items;
    count += docs.length;
    pageToken = response.nextPageToken;
  } while(pageToken);
  console.log("Total: " + count);
}

The do…while loop keeps making a new request as long as the page token from the last request wasn’t empty.

2 Likes

This is exactly what I was looking for and I can’t thank you enough for the help! Ill implement and reach back out when successful!

Edit: @Eric_Koleda I got it to work for my use case and it’s awesome

Thanks again!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.