Solved

getTemplates pagination issue

  • 26 March 2024
  • 5 replies
  • 44 views

Badge

I’m using the klaviyo-api-node  SDK to try and paginate templates for importing in an integration. I’m running into a problem where the `prev` link in the response is always set to `null` like so:

{
"self": "https://a.klaviyo.com/api/templates/?page[cursor]=bmV4dDo6LXVwZGF0ZWQ6OjIwMjAtMTEtMjAgMjI6Mzk6MTcrMDA6MDA&sort=-updated",
"prev": null,
"next": "https://a.klaviyo.com/api/templates/?page%5Bcursor%5D=bmV4dDo6LXVwZGF0ZWQ6OjIwMTktMDktMjMgMjE6NDY6MjErMDA6MDA&sort=-updated"
}

Every time I make a call to getTemplates, the prev link is null, not just the initial call. Any idea why this could be happening?

icon

Best answer by Sujal 26 March 2024, 06:26

View original

5 replies

Userlevel 3
Badge +9

Welcome to the Klaviyo community, @prelich !

The null prev link in your Klaviyo API response for getTemplates suggests you're on the first page of results. Here's how to handle it:

Understanding the Issue:

  • Klaviyo uses cursor-based pagination.
  • prev points to the previous page based on the current cursor position.
  • Null prev means you're at the first page (no prior results).

Solution:

  1. Specify Starting Cursor (if applicable):

    • If you know there are more pages, include a starting cursor value in your initial getTemplates request. Refer to Klaviyo's API documentation for details on constructing and using the cursor parameter.(Klaviyo API Articles)
  2. Iterate Through Pages (if applicable):

    • Assuming multiple pages, keep making API calls using the provided next link until you receive a response with a null next link (indicating the last page).

By following these steps, you should be able to effectively navigate through paginated Klaviyo template data using the klaviyo-api-node SDK.

Badge

@Sujal That is the approach I am taking:

1. I make a call to getTemplates without a pageCursor
2. That call returns the first page with the corresponding `next` link which I then use in the following getTemplates call.

This does get me to the point where I can navigate through each page using the `next` link, however I cannot navigate backwards as the `prev` link is always null.

Userlevel 3
Badge +9

Ohh I get it @prelich , what you can do is:

  1. Maintain a Cursor List: To achieve "backwards" navigation, consider maintaining a list of cursors you've already used. This allows you to iterate back through the list if needed.

  2. Track Current Position: Keep track of your current position within the list of cursors (e.g., an index).

  3. Navigate Backwards: When you need to navigate back, retrieve the previous cursor from your list based on the current position, and use it in a new getTemplates call.

Badge

@Sujal Hmm that could work, but seems like it should be unnecessary. Should this not be taken care of with the `prev` link? I’m wondering what that link is used for then if it doesn’t satisfy this use case?

Userlevel 3
Badge +9

You're right @prelich , ideally, the prev link should facilitate backwards navigation. However, as you've experienced, it's currently a limitation of the Klaviyo API. Let's hope the dev team to gets to this!

Reply