Skip to main content

Pagination

Learn how to paginate your GET or POST requests


Many of our API endpoints support pagination, a crucial feature when handling large outputs. It helps manage and display results efficiently by breaking them into smaller, manageable batches.

You can provide page and per_page params to the API request, and your results will be split into pages.

  • page — Indicates the page number⁠; defaults to 1.
  • per_page — Indicates the number of results that will be contained in each page; defaults to 128. You can get up to 1,000 results per page.

Creating a pagination request may vary depending on whether you're working with a GET or a POST endpoint.

info

The initialization code used in the following example is outlined in detail on the client installation page.

GET Endpoints

For GET requests, the pagination parameters are included in the query string of the URL.

Below is an example using ListInputs to retrieve inputs from an app, starting at page 2 with 20 results per page.

################################################################
# In this section, we set the user authentication and app ID.
# Change these strings to run your own example.
#################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID) # The userDataObject is required when using a PAT

list_inputs_response = stub.ListInputs(
service_pb2.ListInputsRequest(
user_app_id=userDataObject,
page=2,
per_page=20
),
metadata=metadata
)

if list_inputs_response.status.code != status_code_pb2.SUCCESS:
print(list_inputs_response.status)
raise Exception("List inputs failed, status: " + list_inputs_response.status.description)

print(list_inputs_response)

POST Endpoints

For POST requests, the pagination parameters are included in the request body.

Below is an example using PostAnnotationsSearches to filter your search results by custom concepts, starting at page 2 with 20 results per page.

################################################################################
# In this section, we set the user authentication, app ID, and the concept we
# we want to filter by. Change these strings to run your own example.
################################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change this to filter by your own concept
CONCEPT_ID = 'people'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID) # The userDataObject is required when using a PAT

post_annotations_searches_response = stub.PostAnnotationsSearches(
service_pb2.PostAnnotationsSearchesRequest(
user_app_id=userDataObject,
searches = [
resources_pb2.Search(
query=resources_pb2.Query(
filters=[
resources_pb2.Filter(
annotation=resources_pb2.Annotation(
data=resources_pb2.Data(
concepts=[ # You can search by multiple concepts
resources_pb2.Concept(
id=CONCEPT_ID, # You could search by concept Name as well
value=1 # Value of 0 will search for images that don't have the concept
)
]
)
)
)
]
)
)
],
pagination=service_pb2.Pagination(page=2, per_page=20)
),
metadata=metadata
)

if post_annotations_searches_response.status.code != status_code_pb2.SUCCESS:
raise Exception("Post searches failed, status: " + post_annotations_searches_response.status.description)

print(post_annotations_searches_response)