Retrieving Table IDs through Coda API

I see that I can list all the tables in a doc through the API, but it returns table NAMES and I want table IDs. Is there a way to get the table IDs through the API?

Without seeing what code you are implementing to return the tables, I can’t really ascertain what the problem is.

if you use the endpoint

https://coda.io/apis/v1beta1/docs/{your doc id}/tables

you should receive a json object with an output like:

{
  items: [
    {
      "id": "{TABLE ID}",
      "type": "table",
      "href": "https://coda.io/apis/v1beta1/docs/{DOC ID}/tables/{TABLE ID}",
      "browserLink": "https://coda.io/d/{DOC ID}#_tu{TABLE ID}",
      "name": "{TABLE NAME}",
      "parent": {
        "id": "canvas-{CANVAS ID}",
        "type": "section",
        "href": "https://coda.io/apis/v1beta1/docs/{DOC ID}/sections/canvas-{CANVAS ID}",
        "browserLink": "https://coda.io/d/_d{DOC ID}/_su...",
        "name": "{TABLE NAME}"
      }
    }
  ]
}

you should then be able to access it with a lookup of
items[index]["id"]

Let me know if that helps you out

3 Likes

Hi, @Joshua_Upton, thank you for your reply. It solved my problem. My goal was to print a dataframe that lists the items in my doc, including the item type, name and ID. Below is the code in case others find it helpful.

import pandas as pd
import requests
import json

headers = {'Authorization': 'Bearer '+coda_api_key}

list_uri = f'https://coda.io/apis/v1beta1/docs/'+doc_id+'/tables/'
list_res = requests.get(list_uri, headers=headers).json()
list_item = list_res['items']

# extract_values
def extract_values(obj, key):
    """Pull all values of specified key from nested JSON. This came from this guy: https://hackersandslackers.com/extract-data-from-complex-json-python/ """
    arr = []

    def extract(obj, arr, key):
        """Recursively search for values of key in JSON tree."""
        if isinstance(obj, dict):
            for k, v in obj.items():
                if isinstance(v, (dict, list)):
                    extract(v, arr, key)
                elif k == key:
                    arr.append(v)
        elif isinstance(obj, list):
            for item in obj:
                extract(item, arr, key)
        return arr

    results = extract(obj, arr, key)
    return results

def list_doc_items(item):
    table_types = extract_values(item,'type')
    table_names = extract_values(item,'name')
    table_ids = extract_values(item,'id')
    doc_items = pd.DataFrame({
            'type': table_types,
            'name': table_names,
            'id': table_ids
            })
    return doc_items

doc_items = list_doc_items(list_item)

print(doc_items)
3 Likes