Currently Available: Need a skilled Software Developer for your next project?
Categories
APIs

Modern Trends in API Pagination and Webhooks

When you build or use an HTTP API, your client fetches a large collection in smaller batches through pagination. One service can also send an HTTP webhook message to another service when an event happens, such as a payment update. API owners write clear rules so clients know how to ask for the next batch, confirm that a webhook came from the expected sender, and recover when delivery fails.

API designers do not follow one universal format for pagination or webhooks. Instead, each API defines explicit rules for both. A client either follows a link from the server or sends back a token to get the next batch. Webhook providers publish their own rules for signatures, replay protection, and retries, and their delivery guarantees vary. Because of that, you need to document the exact behavior clients must follow.

Pagination contracts use links or tokens

Pagination specifications cover different parts of how a client requests batches. RFC 8288 defines a standard format for HTTP links, including a rel="next" link, while leaving the pagination method up to the server. JSON:API defines where pagination links go and what to name them, again letting each server choose how to paginate. Google’s AIP-158 guidance uses a different pattern: the server returns a page token, and the client sends that token back to ask for the next batch.

Links and tokens work in different ways. When a server returns a link, the client requests that URL directly. When a server returns a token, the client sends that value back so the server knows where to pick up. For example, GitHub’s REST API instructs clients to follow the URLs in its Link response header. Google’s guidance puts a next_page_token in the response body. Clients should follow the API’s documented rules rather than building their own URLs or guessing which parameter to send next.

An opaque token is a value the client treats as unreadable and passes back unchanged. It hides the server’s internal details about where to continue. AIP-158 recommends keeping tokens opaque and safe to use in URLs. Encoding readable data with base64 does not make a token opaque. A token also does not grant access, so the server must still authorize every request. When a client sends a token, it should keep the other request arguments, such as filters and sort order. Each service decides when tokens expire and whether they depend on request parameters, so your API documentation should spell out those rules.

Choose offsets or cursors for your access pattern

When you use offset or page-number pagination, the client asks for a page by its number. This works well for simple collections and interfaces where users jump straight to a specific page. When you use cursor pagination, the server returns a value that marks where to continue, such as an object ID or an opaque token, and the client sends that value back. Keyset pagination is a type of cursor pagination where the server continues from a chosen sort key.

If your clients need to move through a large or changing collection, cursor or keyset pagination is a strong choice. As a client asks for higher offsets, offset pagination can become less suitable depending on how the server queries the data. When someone adds or deletes records while a client is reading through pages, the records at a given position can also shift. The specifications reviewed here do not offer universal performance benchmarks. API owners should measure their own data stores before promising a specific speed advantage.

A cursor does not freeze the collection. If records change while a client moves through the list, the results depend on how the server builds the cursor, sorts records, and handles consistency. A fixed sort order helps. If the main sort value can repeat, add a unique tie-breaker such as an object ID. Promise snapshot consistency only if your service actually provides it. Microsoft’s Azure Data API builder documentation describes stable traversal for its $after implementation, though other cursor APIs may behave differently.

Clients should rely on the explicit continuation signal to know when to stop. Under AIP-158, a service can return fewer results than the requested page size, including zero, even when more results remain. The client checks the next-page token to decide whether to continue. The number of returned items does not tell the client that it has reached the end. Your contract should also state the page-size limit: AIP-158 gives example guidance of a default of up to 50 items and a maximum of 1,000, though those numbers are not global limits.

Verify webhook messages before processing them

Before your receiver acts on a webhook request, it should confirm that the message came from the expected sender. Providers use different headers and signature methods, so read the sender’s documentation instead of assuming similar-looking headers work the same way. Have your receiver verify the signature using the original raw request body and any metadata the provider says it signs. If your receiver parses the JSON and converts it back to text before checking the signature, it can alter the bytes and cause verification to fail.

A valid signature confirms the message contents, yet an attacker can still send that same valid request again. Your receiver can check the timestamp to limit how long it accepts a signed request, and it can use a stable event or message ID to spot duplicate deliveries. Standard Webhooks describes signing a message ID, an attempt timestamp, and the body, then using the message ID to catch duplicates. Its open specification covers HMAC signatures, which use a shared secret, and asymmetric signatures, which use separate keys. Each option brings different requirements for sharing secrets and managing keys.

Receivers should handle events idempotently, meaning your system produces the same intended result whether it processes an event once or multiple times. Record each processed event ID before you apply effects that must happen only once. The OWASP webhook guidance also recommends HTTPS, replay protection, duplicate detection, and retry controls. Check your provider’s signature documentation to see exactly which fields it signs and what timestamp rules it enforces.

Document delivery behavior and recovery

The sender and receiver need clear rules for how quickly the receiver must respond. Your receiver should acknowledge a valid request right away and pass slower work to a background worker. GitHub, for example, tells webhook consumers to respond within 10 seconds. That limit is GitHub’s own guidance and does not apply to every provider. If your receiver takes longer than the sender allows, the sender may mark the delivery as failed under its documented rules.

Senders often retry to recover from temporary failures, though not every provider does. Standard Webhooks recommends that senders use retries over multiple days with exponential backoff and jitter. Exponential backoff increases the wait between attempts, and jitter varies that wait so retries do not hit the receiver in bursts. By contrast, GitHub’s documentation states that GitHub Marketplace does not resend failed deliveries. That rule applies to GitHub Marketplace and may differ for other GitHub webhook products. Consumers should check each provider’s retry window, success status codes, timeout, event ordering, and options for requesting another delivery.

Because a sender might retry a message or stop after one try, your receiver needs both duplicate detection and a way to recover. Depending on the provider, you might request a new delivery or query the source API for the resource’s current state. Do not assume that a sender delivers each event once, keeps events in order, or retries forever unless the provider explicitly guarantees it.

Event formats and delivery security solve separate problems. CloudEvents defines an event envelope and an HTTP webhook approach, while Standard Webhooks focuses on delivery metadata and signature conventions. The CloudEvents webhook document on the project’s main branch is marked as work in progress, while the project README lists a released webhook document. Supporting one format does not mean a provider supports the other’s conventions. When you design an API, specify the event fields separately from the delivery, authorization, verification, and retry rules.

For a related look at these contract choices, see API design: pagination and webhook contracts. When you plan a webhook retry strategy, you also need to account for the sender’s retry policy and your receiver’s ability to handle duplicates.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *