API Pagination Cursor Invalid Error — What It Means & How to Fix It
About API Pagination Cursor Invalid Error
Fix invalid or expired cursor errors in API pagination when the cursor token is no longer valid for fetching the next page of results. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.
Here are the key things to understand: Cursor-based pagination uses an opaque token to fetch subsequent pages of results. Cursors can become invalid when the underlying data changes, the cursor expires, or the token is malformed. Different from offset-based pagination, cursors reference a specific position in the dataset. Most APIs return 400 Bad Request when an invalid cursor is provided. Understanding these fundamentals will help you diagnose and resolve this issue more effectively.
The most common reasons this occurs include: Cursor has expired after a server-defined TTL (common in high-volume APIs). Underlying dataset was modified (records deleted/inserted) invalidating the cursor position. Cursor token was truncated, URL-decoded incorrectly, or otherwise corrupted. Using a cursor from a different API endpoint or query than the one that generated it. Identifying the root cause is the first step toward finding the right solution.
To resolve this, follow these recommended steps: Start pagination from the beginning by omitting the cursor parameter to get a fresh first page. Ensure cursor tokens are passed exactly as received without URL encoding or decoding modifications. Implement retry logic that resets to the first page when an invalid cursor error is received. If cursors expire quickly, increase polling frequency or cache results to avoid re-pagination. If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.
This article is part of our HTTP Status Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.
Quick Answer
How long do cursors typically last?
It varies by API. Some cursors last minutes, others hours or indefinitely. Check the API documentation for cursor TTL. APIs like Slack and Facebook have short-lived cursors.
Overview
Fix invalid or expired cursor errors in API pagination when the cursor token is no longer valid for fetching the next page of results.
Key Details
- Cursor-based pagination uses an opaque token to fetch subsequent pages of results
- Cursors can become invalid when the underlying data changes, the cursor expires, or the token is malformed
- Different from offset-based pagination, cursors reference a specific position in the dataset
- Most APIs return 400 Bad Request when an invalid cursor is provided
Common Causes
- Cursor has expired after a server-defined TTL (common in high-volume APIs)
- Underlying dataset was modified (records deleted/inserted) invalidating the cursor position
- Cursor token was truncated, URL-decoded incorrectly, or otherwise corrupted
- Using a cursor from a different API endpoint or query than the one that generated it
Steps
- 1Start pagination from the beginning by omitting the cursor parameter to get a fresh first page
- 2Ensure cursor tokens are passed exactly as received without URL encoding or decoding modifications
- 3Implement retry logic that resets to the first page when an invalid cursor error is received
- 4If cursors expire quickly, increase polling frequency or cache results to avoid re-pagination