CodaPHP: A library to easily use Coda API

Released version 0.0.4
All features from API version 0.2.4 can be used now with the library.

1 Like

Can backup all sections and tables by CodaPHP ?

You can get and modify the table data with the API via PHP e.g. in a webpage project or tool. So you could backup some of the result data but you can not backup raw formulas, create sections, controls or automations.

If you want to back up a whole doc, you can just duplicate the doc file in GDrive I guess :thinking:

Hi Daniel,

This is great. I’ve just discovered Coda and am trying to update tables via a small bit of php. I am afriad my php is based on searches and trail and effort.

Is there anyway in referencing the last row written to a table? I can see it in the listRows var_dump. As in [0] position in the array. However I can not work out how to get to it.

My table is a simple timestamp that creates a row when a IoT Amp monitor trips. It then trips again when the power goes off. This should result in one row wiht time on and time off. I can do the add bit but not the update last record written.

No worries if you can’t help and thanks for excellent CodaPHP.

Francis

1 Like

Hey @Francis_Hampden , happy to help :slight_smile: Hope I’ve got you right :thinking:

It should work like this:

$doc = '<DOC ID>';
$table = '<TABLE ID>';

// Turn on
$result = $coda->insertRows($doc, $table, ['on'=>'2020-05-19T19:45:00']);

// Turn off
$lastRow = $coda->listRows($doc, $table)['items'][0]['id'];
$response = $coda->updateRow($doc, $table, $lastRow, ['off'=>'2020-05-19T19:52:00']);

Bildschirmfoto 2020-05-19 um 21.10.49

Let me know if there are problems!

1 Like

Thanks Daniel. The irony was that just after posting to you I worked it out and then got caught up and didn’t update my message. Thank you for solution. Pleased to see I was on the right track.

1 Like

Updated the wrapper to 0.1.0 with the Coda API Version 1.0.0.

This update includes breaking changes:

  • Sections & Folders merged to Pages. All methods like listSections() or getFolder()are now combined into listPages() and getPage()
  • All methods addressing Views are now combined with the Table methods. Just use Table or View Name or ID to perform operations.

Please read the API Update note here.

You can get the new version via Composer or via Github

Let me know if you get stuck somewhere or if anything doesn’t work as expected.

1 Like

Hi Daniel,

I worked a bit with your library back in June/July, when it was still linked with the v1beta1 API and enjoyed the abilities it was giving me. Mid-August, we deployed a new webserver and I’m back at coding the whole interface for reading our Coda DB and extract wanted data to write static pages.

I seem to notice a seem variation with the ListRow function, especially with the pageToken parameter. I have the feeling that previously, the pageToken, limit, sortBy, etc, were all treated on the same level, maybe except for the query which was needed as first parameter (if I understand your comments / code properly). However, in the new version, api v1 and your latest library, the pageToken and limit parameters seem to need to be passed with same priority, not as a chain of parameters. EG: ?limit=200?pageToken=400 instead of ?limit=200&pageToken=200. Is that something you’ve noticed as well?

I’m currently working around the function to see if I can fix it, but yeah, I have a feeling that there something not functioning there.

Best,

Hey @Guillaume_Tremblay, I just checked the pageToken functionality. It worked fine for me when using it this way:

Can you give me more insights what is not working properly so I can try reproduce the error? :slight_smile:

Note that when you use a page token, you can not combine it with a different limit and sortBy parameter. The page Token uses the settings from the first page as far as I know.

Regarding the chaining of parameters, this is just the http query string standard and detached from the API/library. The ? marks the beginning of a parameter list and the & is the seperator between parameters. They are all on the same level.

The library though method is designed “two-level” by making the API parameters an array:
listRows(string DOC, string TABLE, array PARAMETERS)
But this is just for the library user, the library should transmit parameters to the API the right way.

The reason why I made it two-level here is to make it (hopefully) easier for library users to add parameters and be ready for future parameters in the API itself. It is just a libary design question and should be best-practice that way :slight_smile:

EDIT: @oleg I’ve just seen that the API Docs show an example with a page token in the format “20”. Could this be an error/misleading in the Docs? I have a format like this
“eyJsaW1pdCI6MjAsIm9mZnNldCI6MjAsIm1wVmVyc3lvbiI6MzM5LCJzY2hlbWFWZXJzaW9uIjoxNTAsInNvcnRCeSI6ImNyZWF0ZWRBdCIsInVzZUOvbHVtbk5hbWVzIjp0cnVlfQ”

1 Like

Hey @Daniel_Stieber, thanks for the quick reply.

So, it all comes to a misinterpration I did while working with your library and API few weeks ago.

After playing around, I figured out I needed to give both parameters to have it to work. So basically, in a for loop, I used to do like the following X times depending on my safeLimit size and table size:

$availableItems = $coda->listRows($doc, $table, [‘sortBy’ => ‘natural’, ‘limit’ => $safeLimit, ‘pageToken’ => $i*$safeLimit, ‘visibleOnly’ => ‘true’]);

Where safeLimit is obviously the amount of records wanted, and $i*$safeLimit from which record I want to read. For some reasons, this used to work in July with your library 0.0.4 and the Coda API v1beta. Now it doesn’t anymore, returning a 400 Bad request “Invalid pageToken.” error.

While debugging the issue, I figured out that querying the document via a browser like the following was returning the exact same error.
https://coda.io/apis/v1/docs/{docId}/tables/{tableIdOrName}/rowsrows?limit=50&pageToken=50

However, sending the request this way was working. Which is what I was referring to yesterday.
https://coda.io/apis/v1/docs/{docId}/tables/{tableIdOrName}/rowsrows?limit=50?pageToken=50

But thanks to your explanations, I now understand I was doing it in an uncommon way, thinking that I needed to tell the API how many results I wanted, starting from which point. Now I understand that the commands are somehow cummulative, so I adjusted my for loop to send only the limit, sortBy and visibleOnly at first, then sending only the pageToken in the following requests and it works perfectly.

Once again, thanks for the quick reply.
Keep the good work.

1 Like

Hey @Guillaume_Tremblay thanks for the good explaination. With that information I guess there was a change/additional method how pageToken can work (start indicator vs page hash) and they forgot to update it in the offical API docs for api version 1. I never had the pageToken in a real use case so I wouldn’t have noticed the change anyway :slight_smile:

But cool that we figured it out! All the best for your project!

1 Like

I’ve updated CodaPHP to 0.3.0.

It now supports Webhook / Automation triggering, so you can set up your own webhook environment:

$result = $coda->runAutomation('<YOUR DOC ID>', '<THE RULE ID>');
1 Like

I’ve updated CodaPHP to 0.4.0.

It now supports doc & pack analytics, so you can e.g. track your pack installs.

$coda->listPackAnalytics(['workspaceId' => 'ws-1234abcd']);
3 Likes

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