Skip to main content
Solved

getTemplates pagination issue

  • March 26, 2024
  • 5 replies
  • 106 views

Forum|alt.badge.img

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?

Best answer by Sujal

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!

View original
Did this topic or the replies in the thread help you find an answer to your question?

5 replies

Sujal
Expert Problem Solver II
Forum|alt.badge.img+11
  • Expert Problem Solver II
  • 61 replies
  • March 26, 2024

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.


Forum|alt.badge.img
  • Author
  • Contributor I
  • 2 replies
  • March 26, 2024

@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.


Sujal
Expert Problem Solver II
Forum|alt.badge.img+11
  • Expert Problem Solver II
  • 61 replies
  • March 26, 2024

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.


Forum|alt.badge.img
  • Author
  • Contributor I
  • 2 replies
  • March 26, 2024

@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?


Sujal
Expert Problem Solver II
Forum|alt.badge.img+11
  • Expert Problem Solver II
  • 61 replies
  • Answer
  • March 26, 2024

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!