Hi, in the documentation of the kratos rest api /a...
# talk-kratos
d
Hi, in the documentation of the kratos rest api /admin/identities endpoints (https://www.ory.sh/docs/kratos/reference/api#tag/identity) is stated, that page parameter is not sequential. How can a client know, which is the next valid page to query?
h
Hey 👋 Apparently, it’s conveyed in Link header of the response. At least that’s what I can see in response headers.
Copy code
curl -q -H 'Authorization: Bearer '$ORY_PAT $ORY_HTTP_BASE'/admin/identities?per_page=1' -v
…
< link: <https://….projects.oryapis.com/identities?page=0&page_size=1&page_token=eyJvZmZzZXQiOiIwIiwidiI6Mn0&per_page=1>; rel="first",<https://….projects.oryapis.com/identities?page=0&page_size=1&page_token=eyJvZmZzZXQiOiIwIiwidiI6Mn0&per_page=1>; rel="prev"
Would’ve been nice to have this documented though.
Also MDN states that
Link
header is experimental, and some browsers do not support it.
d
hi, thanks for the info! i should be able to parse the link header so this is ok for my case
I tested this but I always only get “first” and “prev” links, no “next”. am I missing something?
h
Looks like you’re getting onto the last page with your request. Do you make a request with an explicit
page
query parameter by any chance?
d
yes, I always added a page param
it gives the same result when querying page 0 or page 1
ah no, 1 and 2
h
Probably we’re expected to call the paginated APIs without page to get the first one (at least I hope so), and then follow the links.
d
I see, I will try that
thanks again
h
You’re welcome!
Interestingly, the docs say that
page=1
is the default, but apparently the responses differ with and without it.
d
yes, i see that as well. Probably safest is to call /identities with perPage and no page to start, then use “next”, until no “next” is present
but if default of page is 1 this would not work. Documentation certainly could be improved here (by someone who understands the correct behaviour)
h
The links Ory gives us back have an additional
page_token
parameter, which seemingly makes
page=1
work properly.
¯\_(ツ)_/¯
b
same issue here. our code base parses the link header and looks for next and last. but there is a bug where the last page is not returned when the end is hit. Any ideas if there is a bug in the codebase? here is the code (using kratos v0.11.1): https://github.com/GaloyMoney/galoy/blob/422f9e66715e29001dbe39abe5ac503a50513cdf/src/services/kratos/identity.ts#L48-L76
Copy code
const identities: Identity[] = []
      let totalCount = 0

      let hasNext = true

      let page: number | undefined = 0

      while (hasNext) {
        // Note: this call is paginated, return 250 records `perPage` by default
        const res = await kratosAdmin.listIdentities({ perPage, page })
        identities.push(...res.data)

        totalCount = Number(res.headers["x-total-count"])

        page = getNextPage(res.headers.link)
        hasNext = page !== undefined && totalCount !== 0
      }

      // * fixes bug in kratos res.headers.link not returning last page properly
      const pageCount = totalCount / perPage
      const isPageCountWholeNumber = pageCount % 1 // 0 means its a whole number, therefore, we can skip because no partial last page of data exists
      if (isPageCountWholeNumber !== 0) {
        const lastPageNum = Math.ceil(totalCount / perPage)
        const lastPageRes = await kratosAdmin.listIdentities({
          perPage,
          page: lastPageNum,
        })
        identities.push(...lastPageRes.data)
      }
h
Hmmm… There is definitely at least one bug there. I have a toy project with 4 identities at the moment, and when I use
page_size=3
I can’t access the 4th identity at all.
d
looks the same for me, maybe should open an issue?
h
We do, but I’m a bit flooded with other things at work to file a properly explained issue. Feel free to create one. Or maybe someone from Ory devs/QAs will see this thread and does it for us 🤞
b
there is an issue already opened here https://github.com/ory/kratos/issues/2834 maybe we can add more details to the issue
d
I finally had some time to look into it (again). For me it looks like a bug in the generation of the link URLs. If I test with 6 total identities in kratos and request with per_page=4 (without page number). I get rel=“next” with page=1 (which returns the same results as without page number). When I then request page=2 I get the missing 2 identities. Have to test what it does with uneven numbers.
same behaviour with 7 total identities and per_page=3. the first “next” points to 1 (even when loading page 1), the last “next” is missing. looks like a off-by one bug.
I added some details to issue 2834