Batch requests
Send multiple API requests in a single call with typed results.
The SDK supports server-side request batching. Group related API calls into a single batch and get back typed results for each. This reduces latency and connection overhead.
batch() — send and await all
Pass an array of route/options tuples. The SDK sends them to the batch endpoint and returns a typed tuple for each:
const [chatResult, profileResult, statsResult] = await client.batch(
{
route: "GET /chats",
options: {
auth: { onlyfansUserId: "123456789" },
query: { limit: 10 },
},
},
{
route: "GET /users/me",
options: { auth: { onlyfansUserId: "123456789" } },
},
{
route: "GET /users/me/stats",
options: { auth: { onlyfansUserId: "123456789" } },
},
)
// Each result is typed as [OfApiError, null] | [null, RouteResponse]
const [chatErr, chats] = chatResult
const [profileErr, profile] = profileResult
const [statsErr, stats] = statsResultEach entry in the result array is independently typed based on the route you specified.
batchStream() — process results as they arrive
batchStream() returns results as an async generator. Results arrive
in the order they complete, not the order you sent them:
for await (const { index, result } of client.batchStream(
{
route: "GET /users/:id",
options: {
auth: { onlyfansUserId: "123456789" },
pathParams: { id: "111" },
},
},
{
route: "GET /users/:id",
options: {
auth: { onlyfansUserId: "123456789" },
pathParams: { id: "222" },
},
},
)) {
const [error, user] = result
if (!error) {
console.log(`Result ${index}: ${user.name}`)
}
}The index field tells you which entry in your original batch this
result corresponds to.
batchWith() — control chunking and staggering
batchWith() lets you control how many requests are sent per batch
call and the delay between chunks:
const results = await client.batchWith(
{ maxPerCall: 25, staggerMs: 500 },
...userIds.map((id) => ({
route: "GET /users/:id" as const,
options: {
auth: { onlyfansUserId: "123456789" },
pathParams: { id },
},
})),
)Prop
Type
batchUsers() — same route across accounts
A common pattern is running the same request for each of your managed
accounts. The batchUsers() method simplifies this:
const accountIds = ["111", "222", "333", "444", "555"]
const results = await client.batchUsers(
"GET /users/:id",
accountIds,
(auth) => ({
auth,
pathParams: { id: auth.onlyfansUserId },
}),
)
// results is Record<accountId, [error, data]>
for (const [accountId, [error, user]] of Object.entries(results)) {
if (!error) {
console.log(`${accountId}: ${user.name}`)
}
}Per-entry retries
Each batch entry can specify its own maxRetries. The server retries
failed entries independently:
const [result] = await client.batch({
route: "GET /users/:id",
options: {
auth: { onlyfansUserId: "123456789" },
pathParams: { id: "111" },
},
maxRetries: 5,
})Limits
- Maximum 50 requests per batch call (use
batchWith()to split across multiple calls automatically) - All requests in a batch share the same API key
- Each request can target a different account ID