Develop Bitrix24 Node.js Backend
Quick Start
The Node.js backend is built with Express and uses @bitrix24/b24jssdk for Bitrix24 interaction.
Key Files
backends/node/api/server.js: Main entry point and API routes.backends/node/api/utils/verifyToken.js: JWT verification middleware.
Creating API Endpoints
Use Express routing and the verifyToken middleware.
import verifyToken from './utils/verifyToken.js';
app.get('/api/my-endpoint', verifyToken, async (req, res) => {
// JWT payload is available in req.user (if verifyToken adds it, check implementation)
// or you can decode it manually if needed
res.json({ data: 'value' });
});
Bitrix24 Interaction (JS SDK)
Use @bitrix24/b24jssdk (specifically B24Hook for backend or B24Frame if rendering UI, but usually backend uses Hook or OAuth).
Initialization
import { B24Hook } from '@bitrix24/b24jssdk';
// Initialize with webhook URL (from env or DB)
const b24 = new B24Hook({
b24Url: 'https://your-portal.bitrix24.com',
userId: 1,
secret: 'webhook_token'
});
Common Operations
const result = await b24.callMethod('crm.deal.list', {
select: ['ID', 'TITLE']
});
const deals = result.getData();
Authentication Flow
- Installation:
/api/installreceives OAuth data. - Token Issue:
/api/getTokenissues a JWT for the frontend usingjsonwebtoken. - Requests: Frontend sends JWT in
Authorizationheader.verifyTokenmiddleware validates it.
Database
- Drivers:
pg(PostgreSQL) ormysql2(MySQL). - Configuration: Based on
DB_TYPEenv var. - Connection:
poolobject inserver.js.
Best Practices
- Middleware: Use
verifyTokenfor protected routes. - Async/Await: Use async/await for database and API calls.
- Environment: Use
process.envfor configuration.
Source: bitrix-tools/b24-ai-starter — distributed by TomeVault.