You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: fern/products/sdks/guides/configure-auto-pagination.mdx
+86-92Lines changed: 86 additions & 92 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,105 +3,30 @@ title: Configure Auto Pagination
3
3
description: Paginate through API responses easily with offset, cursor, and link-based pagination.
4
4
---
5
5
6
-
<Markdownsrc="/snippets/pro-callout.mdx" />
6
+
Fern's auto pagination supports offset-based and cursor-based pagination schemes,
7
+
providing SDK users with simple iterators to loop through all results instead of
8
+
managing pagination complexity manually.
7
9
8
-
Instead of forcing SDK users to learn the intricacies of your pagination system, Fern SDKs will return an iterator so that users can simply loop through all the results.
10
+
This page describes how to configure auto pagination for your API endpoints.
9
11
10
-
<Tabs>
11
-
<Tabtitle="TypeScript"language="typescript">
12
-
13
-
When pagination for an endpoint is configured, the TypeScript SDK method
14
-
will return a `Page<T>` where `T` is the underlying data type. The `Page<T>`
15
-
will implement the `AsyncIterable` interface, allowing you to use it in a
16
-
`for await` loop.
17
-
18
-
Below is an example method signature for a list endpoint:
19
-
```typescript UsersClient.ts {10-13}
20
-
importcorefrom"../core";
21
-
22
-
exportinterfaceUsersClient {
23
-
24
-
/**
25
-
* List all users
26
-
* @paramprops
27
-
* @returns A page of users
28
-
*/
29
-
list(
30
-
request:ListUsersRequest= {},
31
-
requestOptions:core.RequestOptions= {}
32
-
):core.Page<User>;
33
-
}
34
-
```
35
-
36
-
And here is an example of how a user would use the `list` method:
37
-
```typescript
38
-
const response =awaitclient.users.list();
39
-
forawait (const user ofresponse) {
40
-
console.log(user);
41
-
}
42
-
```
43
-
44
-
</Tab>
45
-
<Tabtitle="Python"language="python">
46
-
47
-
When pagination for an endpoint is configured, the Python SDK method
48
-
will return a `Pager[T]` (specifically a `SyncPager[T]` or an `AsyncPager[T]`) where `T` is the underlying data type. The `Pager[T]`
49
-
will implement the `Generator` interface, allowing you to use it in a
50
-
`for ... in` loop.
51
-
52
-
Below is an example method signature for a list endpoint:
To configure auto pagination, specify the dot-access path to the related request
15
+
or response property in the `x-fern-pagination extension` (OpenAPI) or `pagination`
16
+
field (Fern Definition).
89
17
90
-
#### Configuration
18
+
For example, if results of `my_nested_object` are located in the subfield
19
+
`inner_list`, the dot-access path would be `results:
20
+
$response.my_nested_object.inner_list`.
91
21
92
-
Annotate the desired paginated endpoint with the `x-fern-pagination` extension.
93
-
For these fields, you can simply specify the dot-access path to the related request or response property.
94
-
95
-
For example, should the results of the following object be found in the subfield `inner_list`, you would specify `results: $response.my_nested_object.inner_list`.
96
-
97
-
```yaml
22
+
```yaml {7-10}
98
23
MyResponseObject:
99
24
type: object
100
25
properties:
101
26
my_nested_object:
102
27
type: object
103
28
properties:
104
-
inner_list:
29
+
inner_list:# location of results
105
30
type: array
106
31
items:
107
32
$ref: '#/components/schemas/MyObject'
@@ -111,7 +36,7 @@ MyResponseObject:
111
36
<Tabs>
112
37
<Tab title="OpenAPI">
113
38
<CodeBlocks>
114
-
```yaml Offset
39
+
```yaml Offset {6}
115
40
...
116
41
paths:
117
42
/path/to/my/endpoint:
@@ -121,7 +46,7 @@ paths:
121
46
...
122
47
```
123
48
124
-
```yaml Cursor
49
+
```yaml Cursor {8}
125
50
...
126
51
paths:
127
52
/path/to/my/endpoint:
@@ -138,7 +63,7 @@ paths:
138
63
<Tabtitle="Fern Definition">
139
64
<CodeBlocks>
140
65
141
-
```yaml Offset
66
+
```yaml Offset {6}
142
67
service:
143
68
endpoints:
144
69
listWithOffsetPagination:
@@ -147,7 +72,7 @@ service:
147
72
results: $response.data
148
73
```
149
74
150
-
```yaml Cursor
75
+
```yaml Cursor {7}
151
76
service:
152
77
endpoints:
153
78
listWithCursorPagination:
@@ -161,5 +86,74 @@ service:
161
86
</Tab>
162
87
</Tabs>
163
88
89
+
## Generated SDK Usage
90
+
<Tabs>
91
+
<Tab title="TypeScript" language="typescript">
92
+
93
+
Once you've configured pagination for an endpoint, Fern generates a TypeScript
94
+
SDK method that returns a `Page<T>` where `T` is the underlying data type. The
95
+
`Page<T>`will implement the `AsyncIterable` interface, allowing your users to
96
+
use it in a `for await` loop.
97
+
98
+
Example generated method signature:
99
+
```typescript UsersClient.ts {10-13}
100
+
import core from "../core";
101
+
102
+
export interface UsersClient {
103
+
104
+
/**
105
+
* List all users
106
+
* @param props
107
+
* @returns A page of users
108
+
*/
109
+
list(
110
+
request: ListUsersRequest = {},
111
+
requestOptions: core.RequestOptions = {}
112
+
): core.Page<User>;
113
+
}
114
+
```
115
+
116
+
Example of how users would call the `list` method:
117
+
```typescript
118
+
const response = await client.users.list();
119
+
for await (const user of response) {
120
+
console.log(user);
121
+
}
122
+
```
123
+
124
+
</Tab>
125
+
<Tab title="Python" language="python">
126
+
127
+
Once you've configured pagination for an endpoint, Fern generates a Python SDK
128
+
method that returns a `Pager[T]` where `T` is the underlying data type. The
129
+
`Pager[T]`will implement the `Generator` interface, allowing your users to
0 commit comments