B24Hook object is intended exclusively for use on the server.- A webhook contains a secret access key, which MUST NOT be used in client-side code (browser, mobile app).
- For the client side, use
B24Frame.
Quick Overview
Use callBatch() to execute up to 50 API commands in a single request. This is especially useful when you need to retrieve or update large amounts of data while minimizing network requests and adhering to API limits.
// Basic usage
import { B24Hook, EnumCrmEntityTypeId } from '@bitrix24/b24jssdk'
const $b24 = B24Hook.fromWebhookUrl('https://your_domain.bitrix24.com/rest/1/webhook_code/')
const response = await $b24.callBatch({
ServerTime: { method: 'server.time' },
UserProfile: { method: 'user.current' }
})
Method Signature
callBatch(
calls: Array<any> | Record<string, any>,
isHaltOnError?: boolean,
returnAjaxResult?: boolean
): Promise<Result>
Parameters
Return Value
Promise<Result> — a promise that resolves to a Result object. This object provides:
.getData()— returns response data. The structure depends on the call parameters..isSuccess: boolean— a flag indicating the successful completion of the request..getErrorMessages(): string[]— an array of error messages.
Key Concepts
Batch Request Formats
- Object with named commands:
{ deals: { method: 'crm.item.list', params: { entityTypeId: EnumCrmEntityTypeId.deal, select: ['id'] } }, contacts: ['crm.item.list', { entityTypeId: EnumCrmEntityTypeId.contact, select: ['id'] }] }
WhenreturnAjaxResult: true,Record<string, AjaxResult>is returned. - Array of arrays:
[ ['crm.item.list', { entityTypeId: EnumCrmEntityTypeId.deal, select: ['id']'] }], ['crm.item.list', { entityTypeId: EnumCrmEntityTypeId.contact, select: ['id'] }] ]
WhenreturnAjaxResult: true,AjaxResult[]is returned. - Array of objects:
[ { method: 'crm.item.list', params: { entityTypeId: EnumCrmEntityTypeId.deal, select: ['id']'] } }, { method: 'crm.item.list', params: { entityTypeId: EnumCrmEntityTypeId.contact, select: ['id']'] } } ]
WhenreturnAjaxResult: true,AjaxResult[]is returned.
Links between teams
In batch queries, you can use the results of one command as parameters for another using a special syntax:
await $b24.callBatch({
FirstDeal: {
method: 'crm.item.list',
params: { entityTypeId: EnumCrmEntityTypeId.deal, order: { id: 'desc' }, select: ['id'] }
},
DealTasks: {
method: 'tasks.task.list',
params: {
filter: {
UF_CRM_TASK: 'D_$result[FirstDeal][items][0][id]' // Link to the result of the first command
}
}
}
})
Important Limitations
- Maximum 50 commands in a single batch request.
- Total execution time must not exceed Bitrix24 limits (usually 30-60 seconds).
- API Limits: A batch request is counted as a single API request, but each command within it counts toward the overall operation limits.
- Execution Order: Commands in a batch are executed sequentially in the order they are specified.
Error Handling
Always check the result using isSuccess and handle errors.
const response: Result<Record<string, AjaxResult>> = await $b24.callBatch({
ServerTime: { method: 'server.time' }
}, true) // isHaltOnError = true
if (!response.isSuccess) {
// Handling API error
console.error(new Error(`API Error: ${response.getErrorMessages().join('; ')}`))
return
}
// Working with a successful result
const data = response.getData()
Examples
Batch request in object format
Using returnAjaxResult for fine-grained control
Combined package with links between commands
Alternatives and Recommendations
- To get the entire list in memory: Use
callFastListMethod, which returns the entire array of data. - For step-by-step streaming processing of very large lists: Use
fetchListMethod. Returns an async generator that yields data page by page, allowing processing to start before the entire list is loaded and saving memory. - When you need the total item count: Use the regular
callMethodwith manual pagination, ascallFastListMethoddoes not count the total. - To run more commands (more than 50): Use
callBatchByChunk, which automatically splits commands into chunks of 50. - On the client (browser): Use the built-in
B24Frameobject instead ofB24Hook.
References
- AjaxResult — Documentation for the result object.
- Result — Documentation for the result object.
- Official Bitrix24 REST API Documentation — Reference for all available methods.
- Working with Large Volumes of Data — Recommendations for optimizing code to retrieve large data.
fetchListMethod
Use `B24Hook.fetchListMethod()` for step-by-step stream processing of very large data lists. It returns an asynchronous generator that fetches data page by page, allowing processing to begin before the entire list has finished loading and conserving memory. It doesn
callBatchByChunk
Use `B24Hook.callBatchByChunk()` to execute batch requests to the Bitrix24 REST API with any number of commands. The method automatically splits a large list of commands into chunks of 50 commands each (the maximum size of a single batch in Bitrix24) and executes them sequentially. Works only in a server environment.