B24Hook Class
Designed for managing Bitrix24 webhooks. It inherits functionality from AbstractB24 and provides methods for working with authentication via webhooks.
Implements the TypeB24 interface.
TIP
You can test working with B24Hook in this example.
Constructor
constructor(b24HookParams: B24HookParams)The B24HookParams type describes the webhook parameters used to initialize the authorization manager and HTTP client:
| Field | Type | Description |
|---|---|---|
b24Url | string | Bitrix24 portal URL, e.g., https://your-bitrix-portal.bitrix24.com. |
userId | number | User identifier. |
secret | string | Secret key. |
fromWebhookUrl
fromWebhookUrl(url: string): B24HookThis static method creates an instance of B24Hook from full webhook URL.
| param | Type | Description |
|---|---|---|
url | string | Bitrix24 webhook URL, e.g., https://your_domain.bitrix24.com/rest/1/xxx/ |
import { B24Hook } from '@bitrix24/b24jssdk'
const $b24 = B24Hook.fromWebhookUrl('https://your_domain.bitrix24.com/rest/1/xxxx/')Methods
INFO
Implements the TypeB24 interface.
offClientSideWarning
offClientSideWarning(): voidDisables warning about front-end query execution.
WARNING
You should not use hook requests on the front-end side. This operation is unsafe. Instead, use the back-end.
Usage
This code creates an instance of B24Hook to interact with the Bitrix24 API and performs a batch request to retrieve a list of companies, sorting them by ID in descending order.
The retrieved data is transformed into an array of objects with fields id, title, and createdTime, after which the results are logged to the console, and in case of an error, an error message is displayed.
import {
B24Hook,
Text,
EnumCrmEntityTypeId,
LoggerBrowser,
Result,
type ISODate
} from '@bitrix24/b24jssdk'
const $logger = LoggerBrowser.build(
'MyApp',
import.meta.env?.DEV === true
)
const $b24 = B24Hook.fromWebhookUrl('https://your_domain.bitrix24.com/rest/1/xxxx/')
const $b24 = new B24Hook({
b24Url: 'https://your_domain.bitrix24.com',
userId: 123,
secret: 'k32t88gf3azpmwv3',
})
// $b24.offClientSideWarning() ////
/**
* @memo You should not use hook requests on the front-end side.
* This operation is unsafe. Instead, use the back-end.
*/
$b24.callBatch({
CompanyList: {
method: 'crm.item.list',
params: {
entityTypeId: EnumCrmEntityTypeId.company,
order: { id: 'desc' },
select: [
'id',
'title',
'createdTime'
]
}
}
}, true)
.then((response: Result) => {
const data = response.getData()
const dataList = (data.CompanyList.items || []).map((item: any) => {
return {
id: Number(item.id),
title: item.title,
createdTime: Text.toDateTime(item.createdTime as ISODate)
}
})
$logger.info('response >> ', dataList)
})
.catch((error) => {
$logger.error(error)
})
.finally(() => {
$logger.info('load >> stop ')
})