move-langaptos

How do I get all the keys in a table?


Assume I have a table deployed on chain. I know if I have a key, I can lookup the value like this:

Given a key, how do I read the value from a table via the API?

But what if I don't know the key? How do I lookup all the keys in a table via the API?


Solution

  • Indexer API

    You can make a query to the table_items table. You will need to paginate to get all the items:

    query MyQuery {
      table_items(
        where: {table_handle: {_eq: "0xd71c4400706e2addf04f8728fbc4f0a87d0612456cdcec95ae8fea0a4bd31b67"}}
        limit: 100
        offset: 0
      ) {
        key
      }
    }
    

    Each request increase offset by limit (e.g. in this case, by 100 each time). Continue until the response has fewer than 100 items.

    Learn about the indexer API here: https://aptos.dev/indexer/api/.

    Node API

    Unfortunately with regular tables there is no way to do this via the node API right now.

    Alternatives