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 callMethod for direct REST API calls.
The method returns a Promise with an AjaxResult object containing the response data, status, and methods for error handling and pagination.
// Basic usage
import { B24Hook } from '@bitrix24/b24jssdk'
const $b24 = B24Hook.fromWebhookUrl('https://your_domain.bitrix24.com/rest/1/webhook_code/')
const response = await $b24.callMethod('method.api', {parameters})
Method Signature
callMethod<T = unknown>(
method: string,
params?: object
): Promise<AjaxResult<T>>
Parameters
Return Value
Promise<AjaxResult<T>> — a promise that resolves to an AjaxResult object.
This object provides:
.getData(): Payload<T>— returns the full API response..isSuccess: boolean— flag indicating successful request execution..getErrorMessages(): string[]— array of error messages..isMore(): boolean— indicates if more data is available during pagination.
Key Concepts
Pagination
API methods that return lists use pagination.
- Fixed page size: 50 records.
- Offset management: The
params.startparameter sets the offset.params.start: 0— first page (records 1-50).params.start: 50— second page (records 51-100).- Formula:
params.start = (Page_Number - 1) * 50.
- Retrieving all data without pagination: Set
params.start: -1for efficient export of large data volumes. This disables the total count calculation, significantly speeding up the process.- We will still receive 50 records per request.
- The idea is to change the filter condition on the ID field (
'>id': lastId) and continue calling the request until an empty response is received.
Error Handling
Always check the result using isSuccess and handle errors.
const response = await $b24.callMethod('some.method')
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
Getting a CRM Item
Getting a Single User
Getting a Single Task
@todo off preview
Getting All Data with Pagination
Efficient Export of All Data
No Pagination, start=-1
Alternatives and Recommendations
- For working with lists: Instead of manually managing pagination in
callMethod, use:callFastListMethod— automatically retrieves all pages and returns a single result.fetchListMethod— returns an async generator for step-by-step processing of large lists.
- For batch operations: Use
callBatchto execute up to 50 commands in a single request. - On the client (browser): Use the built-in
B24Frameobject instead ofB24Hook.
References
- AjaxResult — 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.
B24Hook
Class for secure interaction with the Bitrix24 REST API from server-side code. Contains methods for creating authorized requests via webhooks. Works only in server environment.
callFastListMethod
Use `B24Hook.callFastListMethod()` to quickly fetch all list items from the Bitrix24 REST API. The method automatically performs all necessary requests to retrieve the complete dataset using an optimized approach that does not count the total number of items, significantly speeding up the process when working with large data volumes. Works only in server environment.