1C integration with web application: practical guide
Integrating 1C:Enterprise with web applications is a common business task. In this article, I'll cover different integration methods and show practical examples.
1C integration methods
1. REST API (HTTP services)
The most modern and recommended approach. 1C provides HTTP services for data exchange.
Setup in 1C:
1// Create HTTP service in configurator 2// Request handler 3Function ProcessRequest(Request) Export 4 RequestParams = Request.RequestParams; 5 6 If Request.Method = "GET" Then 7 // Get data 8 Return GetData(RequestParams); 9 ElseIf Request.Method = "POST" Then 10 // Create/update data 11 Return CreateData(Request.BodyAsString()); 12 EndIf; 13EndFunction
Integration in Next.js:
1// lib/1c-client.ts 2export class OneCClient { 3 private baseUrl: string; 4 private credentials: string; 5 6 constructor(baseUrl: string, username: string, password: string) { 7 this.baseUrl = baseUrl; 8 this.credentials = Buffer.from(`${username}:${password}`).toString('base64'); 9 } 10 11 async getData(endpoint: string): Promise<unknown> { 12 const response = await fetch(`${this.baseUrl}/${endpoint}`, { 13 headers: { 14 'Authorization': `Basic ${this.credentials}`, 15 'Content-Type': 'application/json', 16 }, 17 }); 18 19 if (!response.ok) { 20 throw new Error(`1C API error: ${response.statusText}`); 21 } 22 23 return response.json(); 24 } 25 26 async postData(endpoint: string, data: unknown): Promise<unknown> { 27 const response = await fetch(`${this.baseUrl}/${endpoint}`, { 28 method: 'POST', 29 headers: { 30 'Authorization': `Basic ${this.credentials}`, 31 'Content-Type': 'application/json', 32 }, 33 body: JSON.stringify(data), 34 }); 35 36 if (!response.ok) { 37 throw new Error(`1C API error: ${response.statusText}`); 38 } 39 40 return response.json(); 41 } 42}
2. COM connection
For local integrations, COM connection can be used (Windows only).
1// Requires Node.js on Windows 2import { exec } from 'child_process'; 3 4// Usage via external program or COM object 5// Not recommended for production
3. File exchange
Simple method via XML/JSON file exchange.
1// lib/1c-file-exchange.ts 2import fs from 'fs'; 3import path from 'path'; 4 5export async function read1CExport(filePath: string): Promise<unknown> { 6 const content = await fs.promises.readFile(filePath, 'utf-8'); 7 return JSON.parse(content); 8} 9 10export async function write1CImport(data: unknown, filePath: string): Promise<void> { 11 await fs.promises.writeFile(filePath, JSON.stringify(data, null, 2), 'utf-8'); 12}
Practical example: product synchronization
API Route in Next.js
1// app/api/1c/sync-products/route.ts 2import { NextResponse } from 'next/server'; 3import { OneCClient } from '@/lib/1c-client'; 4 5const oneC = new OneCClient( 6 process.env.ONE_C_BASE_URL!, 7 process.env.ONE_C_USERNAME!, 8 process.env.ONE_C_PASSWORD!, 9); 10 11export async function POST() { 12 try { 13 // Get products from 1C 14 const products = await oneC.getData('catalog/products'); 15 16 // Sync with database 17 // ... sync logic 18 19 return NextResponse.json({ 20 success: true, 21 synced: products.length 22 }); 23 } catch (error) { 24 return NextResponse.json( 25 { error: 'Sync failed' }, 26 { status: 500 } 27 ); 28 } 29}
Error handling and retries
1async function syncWithRetry( 2 fn: () => Promise<unknown>, 3 maxRetries = 3 4): Promise<unknown> { 5 for (let i = 0; i < maxRetries; i++) { 6 try { 7 return await fn(); 8 } catch (error) { 9 if (i === maxRetries - 1) throw error; 10 await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1))); 11 } 12 } 13 throw new Error('Max retries exceeded'); 14}
Security
- Use HTTPS for all requests
- Store credentials in environment variables
- Limit access by IP
- Use tokens instead of basic authentication
- Log all operations
Conclusion
Integrating 1C with web applications requires understanding both systems' architecture. REST API is the most reliable and scalable approach. Proper error handling and security are critical for production.
