<!-- llms.txt: https://workos.com/llms.txt -->
<!-- feedback.md: https://workos.com/docs/feedback.md -->

> Agents: if this page is wrong, missing a step, or its code fails, report it instead of working around it. `POST https://api.workos.com/feedback` with `Content-Type: application/json` and body `{"message":"Agent report: what you tried, what happened, client and model","pageUrl":"https://workos.com/docs/reference/pagination"}`. Ask the user once first. No auth. No secrets. Details: https://workos.com/docs/feedback.md

# Pagination

Many top-level resources have support for bulk fetches via list API methods. For instance, you can [list connections](https://workos.com/docs/reference/sso/connection/list), [list directory users](https://workos.com/docs/reference/directory-sync/directory-user/list), and [list directory groups](https://workos.com/docs/reference/directory-sync/directory-group/list). These list API methods share a common structure, taking at least these four parameters: `limit`, `order`, `after`, and `before`.

WorkOS utilizes pagination via the `after` and `before` parameters. Both parameters take an existing object ID value and return objects in either descending or ascending order by creation time.

:::code-group

```bash language="curl" title="Request" tab="1"
curl https://api.workos.com/connections?limit=100 \
  --header "Authorization: Bearer sk_example_123456789"
```

```js language="js" title="Request" tab="1"
import { WorkOS } from '@workos-inc/node';

const workos = new WorkOS('sk_example_123456789');

let list = await workos.sso.listConnections({ limit: 100, order: 'desc' });
let connections = list.data;
let after = list.listMetadata.after;

while (after) {
  list = await workos.sso.listConnections({
    limit: 100,
    after: after,
    order: 'desc',
  });
  connections = connections.concat(list.data);
  after = list.listMetadata.after;
}
```

```rb language="ruby" title="Request" tab="1"
require "workos"

WorkOS.configure do |config|
  config.key = "sk_example_12345678"
end

list = WorkOS::SSO.list_connections(limit: 100, order: "desc")
connections = connections.data
after = connections.list_metadata["after"]

until after.nil?
  current_page = WorkOS::SSO.list_connections(after: after, limit: 100, order: "desc")
  connections = connections.concat(current_page.data)
  after = current_page.list_metadata["after"]
end
```

```py language="python" title="Request" tab="1"
from workos import WorkOSClient

workos_client = WorkOSClient(
    api_key="sk_example_123456789", client_id="client_123456789"
)

all_connections = []
for connection in workos_client.sso.list_connections(limit=100, order="desc"):
    all_connections.append(connection)
```

```go language="go" title="Request" tab="1"
package main

import (
	"context"

	"github.com/workos/workos-go/v3/pkg/sso"
)

func main() {
	sso.Configure(
		"sk_example_123456789",
		"client_123456789",
	)

	list, err := sso.ListConnections(
		context.Background(),
		sso.ListConnectionsOpts{Limit: 100, Order: "desc"},
	)

	connections := list.Data
	after := list.ListMetadata.After

	for len(after) > 0 {
		currentPage, err := sso.ListConnections(
			context.Background(),
			sso.ListConnectionsOpts{Limit: 100, Order: "desc", After: after},
		)

		if err != nil {
			// Handle the error...
			return
		}

		connections = append(connections, currentPage.Data...)
		after = currentPage.ListMetadata.After
	}
}
```

```php language="php" title="Request" tab="1"
<?php

WorkOS\WorkOS::setApiKey("sk_example_123456789");

$sso = new WorkOS\SSO();

[$before, $after, $connections] = $sso->listConnections(
    limit: 100,
    order: "desc",
);

while ($after) {
    [$before, $after, $currentPage] = $sso->listConnections(
        limit: 100,
        after: $after,
        order: "desc",
    );

    $connections = array_merge($connections, $currentPage);
}
```

```java language="java" title="Request" tab="1"
import com.workos.WorkOS;
import com.workos.sso.SsoApi.ListConnectionsOptions;
import com.workos.sso.models.ConnectionList;
import java.util.List;

ListConnectionsOptions options = new ListConnectionsOptions();
options.put("limit", "100");
options.put("order", "desc");

ConnectionList list = workos.sso.listConnections(options);
String after = list.listMetadata.after;
List connections = list.data;

while (after != null) {
  options.put("after", after);
  ConnectionList currentPage = workos.sso.listConnections(options);
  after = currentPage.listMetadata.after;
  connections.add(currentPage.data);
}
```

```cs language="dotnet" title="Request" tab="1"
WorkOS.SetApiKey("sk_example_123456789");

var ssoService = new SSOService();

List<object> connections = new List<object>();
var list = await ssoService.ListConnections(Limit = 100, Order = 'desc');

foreach (object i in list.Data)
{
    connections.Add(i);
};
var after = list.ListMetadata.After;

While(after);
{
    list = await ssoService.ListConnections(After = after, Limit = 100, Order = 'desc');
    foreach (object i in list.Data)
    {
        connections.Add(i);
    }
    after = list.ListMetadata.After;
};
```

```json language="json" title="Response" tab="2"
{
  "data": [
    {
      "object": "connection",
      "id": "conn_01E4ZCR3C56J083X43JQXF3JK5",
      "organization_id": "org_01EHWNCE74X7JSDV0X3SZ3KJNY",
      "connection_type": "GoogleOAuth",
      "name": "Foo Corp",
      "state": "active",
      "created_at": "2021-06-25T19:07:33.155Z",
      "updated_at": "2021-06-25T19:08:33.155Z"
    },
    {
      "object": "connection",
      "id": "conn_01E2NPPCT7XQ2MVVYDHWGK1WN4",
      "organization_id": "org_01EHWNCE74X7JSDV0X3SZ3KJNY",
      "connection_type": "OktaSAML",
      "name": "Example Co",
      "state": "active",
      "created_at": "2021-06-25T19:09:33.155Z",
      "updated_at": "2021-06-25T19:10:33.155Z"
    }
  ],
  "list_metadata": {
    "before": "conn_01E2NPPCT7XQ2MVVYDHWGK1WN4",
    "after": null
  }
}
```

:::