Skip to content
This repository has been archived by the owner on Jun 23, 2021. It is now read-only.

List API Pagination

James Hood edited this page Oct 14, 2019 · 2 revisions

The ListApplications API can return a lot of results if the user creates many applications. Because of this, the ListApplications API is defined as a paginated API, meaning there is a limit on the number of results the API will return in a single call, but in the event that more results are available, it also returns a next token that can be passed to a subsequent API call to continue listing more applications. Pagination of list APIs is a best practice to ensure:

  1. the response won't exceed the length limit of the framework that is handling it.
  2. the caller and service have precise control over the maximum number of results one API call can return.

When it comes to implementing next token, there are additional best practices:

  1. Security - The token should be encrypted to avoid leaking internal implementation details to users. This prevents malicious users from generating a valid next token to hack the service. AWS Key Management Service (KMS) is a good choice for encrypting and decrypting the next token.
  2. Expiration - The token should expire after a finite time. This prevents malicious users from using a valid token to attack the service. A time-to-live (TTL) for token expiration is included when generating the next token.

Examples in this project:

  1. The ListApplications API definition includes a nextToken parameter in the request and response objects.
  2. Internally, the backend service stores application data in a DynamoDB table. The ListApplications implementation uses DynamoDB's Query API on the table hash key to list application records.
  3. DynamoDB's Query API has a nextToken-like value it returns called lastEvaluatedKey. If this value is returned by the API, it is serialized into a JSON string, a TTL value is added, and then the value is encrypted using a KMS key. When a nextToken value is passed to the ListApplications API, the reverse is performed on the given value. If valid, the value is passed to DynamoDB's Query API as the exclusiveStartKey parameter.
Clone this wiki locally