cbamsdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +74 -0
  3. package/index.d.ts +104 -0
  4. package/index.js +188 -0
  5. package/package.json +37 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 CBAM Platform
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # cbamsdk
2
+
3
+ Official Node.js SDK for the CBAM Platform API. Integrate EU Carbon Border Adjustment Mechanism (CBAM) reporting, validation, and AI-powered document extraction into your Node.js or TypeScript applications with ease.
4
+
5
+ ## Installation
6
+
7
+ \`\`\`bash
8
+ npm install cbamsdk
9
+ \`\`\`
10
+
11
+ ## Quick Start
12
+
13
+ \`\`\`javascript
14
+ const { CBAMClient } = require('cbamsdk');
15
+
16
+ // Initialize the client
17
+ // Set sandbox: true to use the testing environment
18
+ const client = new CBAMClient('cbam_sk_sandbox_YOUR_KEY', { sandbox: true });
19
+
20
+ async function run() {
21
+ try {
22
+ // 1. Get Default Emission Values
23
+ const defaults = await client.getDefaultValues();
24
+ console.log("Default Values:", defaults);
25
+
26
+ // 2. Perform AI Intake from a Commercial Invoice
27
+ const intakeOptions = {
28
+ payloadFormat: 'json',
29
+ language: 'en',
30
+ requestedOutputs: 'official_xml,zip'
31
+ };
32
+
33
+ // Provide the absolute or relative path to the PDF invoice
34
+ const result = await client.directIntake('./invoice.pdf', intakeOptions);
35
+ console.log("Intake Request ID:", result.data.request_id);
36
+
37
+ // 3. Download a completed report
38
+ // const stream = await client.downloadReport('req-sandbox-12345');
39
+ // stream.pipe(fs.createWriteStream('report.zip'));
40
+
41
+ } catch (error) {
42
+ console.error(error.message);
43
+ }
44
+ }
45
+
46
+ run();
47
+ \`\`\`
48
+
49
+ ## Advanced Usage (ERP Integration)
50
+
51
+ For high-security environments like ERP gateways, use the \`submitIntegration\` method which supports HMAC signatures and custom Idempotency Keys.
52
+
53
+ \`\`\`javascript
54
+ const result = await client.submitIntegration(
55
+ './fatura.pdf',
56
+ { external_reference: 'ERP-INV-001', requested_outputs: ['official_xml'] },
57
+ { schema_version: 'cbam-intake-v1', target_country: 'DE' }, // optional payload
58
+ {
59
+ 'X-CBAM-Timestamp': '1778064000',
60
+ 'X-CBAM-Nonce': 'ERP-INV-001-01',
61
+ 'X-CBAM-Signature': 'a1b2c3d4...',
62
+ 'Idempotency-Key': 'ERP-INV-001'
63
+ }
64
+ );
65
+ \`\`\`
66
+
67
+ ## Features
68
+ - Full API Wrapper for \`/api/v1/cbam/*\`
69
+ - Automatic \`multipart/form-data\` handling for file uploads
70
+ - Clean error handling wrapping HTTP responses
71
+ - Production and Sandbox ready
72
+
73
+ ## License
74
+ MIT
package/index.d.ts ADDED
@@ -0,0 +1,104 @@
1
+ export interface CBAMOptions {
2
+ /**
3
+ * Set to true to use the Sandbox environment instead of Production.
4
+ * Default: false
5
+ */
6
+ sandbox?: boolean;
7
+
8
+ /**
9
+ * Override the default API base URL.
10
+ */
11
+ baseUrl?: string;
12
+ }
13
+
14
+ export interface IntakeOptions {
15
+ /**
16
+ * JSON payload as an object or string.
17
+ */
18
+ payload?: any;
19
+
20
+ /**
21
+ * Payload format: 'json' or 'xml'.
22
+ */
23
+ payloadFormat?: 'json' | 'xml';
24
+
25
+ /**
26
+ * Target language for the output (e.g., 'en', 'tr', 'de').
27
+ */
28
+ language?: string;
29
+
30
+ /**
31
+ * Comma-separated list of requested outputs (e.g., 'official_xml,zip').
32
+ */
33
+ requestedOutputs?: string;
34
+ }
35
+
36
+ export interface SecurityHeaders {
37
+ 'X-CBAM-Signature'?: string;
38
+ 'X-CBAM-Timestamp'?: string;
39
+ 'X-CBAM-Nonce'?: string;
40
+ 'Idempotency-Key'?: string;
41
+ [key: string]: string | undefined;
42
+ }
43
+
44
+ export declare class CBAMClient {
45
+ /**
46
+ * Initialize the CBAM API Client.
47
+ * @param apiKey Your CBAM API Secret Key (e.g. cbam_live_sk_...)
48
+ * @param options Configuration options for the client.
49
+ */
50
+ constructor(apiKey: string, options?: CBAMOptions);
51
+
52
+ /**
53
+ * Validate a CBAM payload.
54
+ * @param payload The CbamTurkIntake JSON payload.
55
+ */
56
+ validate(payload: any): Promise<any>;
57
+
58
+ /**
59
+ * Calculate emissions and generate report.
60
+ * @param payload The calculation request payload.
61
+ */
62
+ calculate(payload: any): Promise<any>;
63
+
64
+ /**
65
+ * Get default EU emission values.
66
+ */
67
+ getDefaultValues(): Promise<any>;
68
+
69
+ /**
70
+ * Get report generation history.
71
+ * @param page Page number (default 1).
72
+ * @param limit Items per page (default 10).
73
+ */
74
+ getHistory(page?: number, limit?: number): Promise<any>;
75
+
76
+ /**
77
+ * Download a completed report ZIP package or PDF.
78
+ * @param reportId The ID of the completed report.
79
+ * @returns A readable stream of the downloaded file.
80
+ */
81
+ downloadReport(reportId: string): Promise<any>;
82
+
83
+ /**
84
+ * Perform direct intake for smart data extraction (Sandbox / Direct API).
85
+ * @param invoicePath Absolute or relative path to the invoice PDF/XML on disk.
86
+ * @param options Payload and output configurations.
87
+ */
88
+ directIntake(invoicePath: string, options?: IntakeOptions): Promise<any>;
89
+
90
+ /**
91
+ * Advanced Integration Submissions (For ERP / Gateway flows).
92
+ * Supports HMAC signatures and advanced metadata.
93
+ * @param invoicePath Absolute or relative path to the invoice file.
94
+ * @param metadata Metadata object (e.g., external_reference).
95
+ * @param payload Optional CbamTurkIntake payload (object or string).
96
+ * @param securityHeaders Headers for security (Signature, Nonce, Timestamp).
97
+ */
98
+ submitIntegration(
99
+ invoicePath: string,
100
+ metadata?: any,
101
+ payload?: any,
102
+ securityHeaders?: SecurityHeaders
103
+ ): Promise<any>;
104
+ }
package/index.js ADDED
@@ -0,0 +1,188 @@
1
+ const axios = require('axios');
2
+ const FormData = require('form-data');
3
+ const fs = require('fs');
4
+
5
+ class CBAMClient {
6
+ /**
7
+ * Initialize the CBAM Node.js SDK
8
+ * @param {string} apiKey - Your API Secret Key (e.g. cbam_live_sk_...)
9
+ * @param {Object} options - Configuration options
10
+ * @param {boolean} options.sandbox - Set to true to use the sandbox environment
11
+ * @param {string} options.baseUrl - Override the default base URL
12
+ */
13
+ constructor(apiKey, options = {}) {
14
+ if (!apiKey) {
15
+ throw new Error("API Key is required to initialize CBAMClient.");
16
+ }
17
+
18
+ const isSandbox = options.sandbox === true;
19
+ const defaultBaseUrl = isSandbox
20
+ ? 'https://sandbox.cbam.com.tr/api/v1'
21
+ : 'https://api.cbam.com.tr/api/v1';
22
+
23
+ this.baseUrl = options.baseUrl || defaultBaseUrl;
24
+ this.apiKey = apiKey;
25
+
26
+ this.client = axios.create({
27
+ baseURL: this.baseUrl,
28
+ headers: {
29
+ 'Authorization': `Bearer ${this.apiKey}`,
30
+ 'Content-Type': 'application/json'
31
+ }
32
+ });
33
+ }
34
+
35
+ async _get(path, params = {}) {
36
+ try {
37
+ const response = await this.client.get(path, { params });
38
+ return response.data;
39
+ } catch (error) {
40
+ this._handleError(error);
41
+ }
42
+ }
43
+
44
+ async _post(path, data = {}) {
45
+ try {
46
+ const response = await this.client.post(path, data);
47
+ return response.data;
48
+ } catch (error) {
49
+ this._handleError(error);
50
+ }
51
+ }
52
+
53
+ async _postMultipart(path, formData, extraHeaders = {}) {
54
+ try {
55
+ const response = await this.client.post(path, formData, {
56
+ headers: {
57
+ ...formData.getHeaders(),
58
+ ...extraHeaders
59
+ }
60
+ });
61
+ return response.data;
62
+ } catch (error) {
63
+ this._handleError(error);
64
+ }
65
+ }
66
+
67
+ _handleError(error) {
68
+ if (error.response) {
69
+ throw new Error(`CBAM API Error (${error.response.status}): ${JSON.stringify(error.response.data)}`);
70
+ } else if (error.request) {
71
+ throw new Error("CBAM API Error: No response received from server.");
72
+ } else {
73
+ throw new Error(`CBAM API Error: ${error.message}`);
74
+ }
75
+ }
76
+
77
+ // -------------------------
78
+ // CORE ENDPOINTS
79
+ // -------------------------
80
+
81
+ /**
82
+ * Validate a CBAM payload.
83
+ * @param {Object} payload - The CbamTurkIntake JSON payload
84
+ */
85
+ async validate(payload) {
86
+ return this._post('/cbam/validate', payload);
87
+ }
88
+
89
+ /**
90
+ * Calculate emissions and generate report
91
+ * @param {Object} payload - The calculation request payload
92
+ */
93
+ async calculate(payload) {
94
+ return this._post('/cbam/calculate', payload);
95
+ }
96
+
97
+ /**
98
+ * Get default EU emission values
99
+ */
100
+ async getDefaultValues() {
101
+ return this._get('/cbam/default-values');
102
+ }
103
+
104
+ /**
105
+ * Get report generation history
106
+ * @param {number} page
107
+ * @param {number} limit
108
+ */
109
+ async getHistory(page = 1, limit = 10) {
110
+ return this._get('/cbam/history', { page, limit });
111
+ }
112
+
113
+ /**
114
+ * Download a completed report ZIP package or PDF
115
+ * @param {string} reportId
116
+ * @returns {Stream} Returns the response stream
117
+ */
118
+ async downloadReport(reportId) {
119
+ try {
120
+ const response = await this.client.get(`/cbam/download/${reportId}`, {
121
+ responseType: 'stream'
122
+ });
123
+ return response.data;
124
+ } catch (error) {
125
+ this._handleError(error);
126
+ }
127
+ }
128
+
129
+ // -------------------------
130
+ // INTAKE & INTEGRATION
131
+ // -------------------------
132
+
133
+ /**
134
+ * Perform direct intake for smart data extraction (Sandbox / Direct API)
135
+ * @param {string} invoicePath - Absolute or relative path to the invoice PDF/XML
136
+ * @param {Object} options - Payload and output configurations
137
+ */
138
+ async directIntake(invoicePath, options = {}) {
139
+ const form = new FormData();
140
+ if (invoicePath && fs.existsSync(invoicePath)) {
141
+ form.append('invoice', fs.createReadStream(invoicePath));
142
+ } else if (invoicePath) {
143
+ throw new Error(`Invoice file not found at path: ${invoicePath}`);
144
+ }
145
+
146
+ if (options.payload) {
147
+ const payloadStr = typeof options.payload === 'object' ? JSON.stringify(options.payload) : options.payload;
148
+ form.append('payload', payloadStr);
149
+ }
150
+ if (options.payloadFormat) form.append('payload_format', options.payloadFormat);
151
+ if (options.language) form.append('language', options.language);
152
+ if (options.requestedOutputs) form.append('requested_outputs', options.requestedOutputs);
153
+
154
+ return this._postMultipart('/cbam/intake', form);
155
+ }
156
+
157
+ /**
158
+ * Advanced Integration Submissions (For ERP / Gateway flows)
159
+ * Supports HMAC signatures and advanced metadata.
160
+ * @param {string} invoicePath
161
+ * @param {Object} metadata
162
+ * @param {Object|string} payload
163
+ * @param {Object} securityHeaders - X-CBAM-Signature, X-CBAM-Timestamp, etc.
164
+ */
165
+ async submitIntegration(invoicePath, metadata = {}, payload = null, securityHeaders = {}) {
166
+ const form = new FormData();
167
+
168
+ if (invoicePath && fs.existsSync(invoicePath)) {
169
+ form.append('invoice', fs.createReadStream(invoicePath));
170
+ } else if (invoicePath) {
171
+ throw new Error(`Invoice file not found at path: ${invoicePath}`);
172
+ }
173
+
174
+ if (metadata) {
175
+ form.append('metadata', typeof metadata === 'object' ? JSON.stringify(metadata) : metadata);
176
+ }
177
+
178
+ if (payload) {
179
+ const payloadStr = typeof payload === 'object' ? JSON.stringify(payload) : payload;
180
+ form.append('payload', payloadStr);
181
+ form.append('payload_format', 'json');
182
+ }
183
+
184
+ return this._postMultipart('/integrations/cbam/submissions', form, securityHeaders);
185
+ }
186
+ }
187
+
188
+ module.exports = { CBAMClient };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "cbamsdk",
3
+ "version": "1.0.0",
4
+ "description": "Official Node.js SDK for the CBAM Platform API. Integrate EU Carbon Border Adjustment Mechanism (CBAM) reporting, validation, and AI-powered document extraction.",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "keywords": [
11
+ "cbam",
12
+ "carbon",
13
+ "emissions",
14
+ "eu",
15
+ "reporting",
16
+ "api",
17
+ "sdk",
18
+ "sustainability",
19
+ "invoice",
20
+ "ocr"
21
+ ],
22
+ "author": "CBAM Platform <admin@dynoblastizmir.com.tr>",
23
+ "license": "MIT",
24
+ "type": "commonjs",
25
+ "dependencies": {
26
+ "axios": "^1.18.1",
27
+ "form-data": "^4.0.6"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/cbam-platform/cbam-node-sdk.git"
32
+ },
33
+ "bugs": {
34
+ "url": "https://github.com/cbam-platform/cbam-node-sdk/issues"
35
+ },
36
+ "homepage": "https://cbam.com.tr"
37
+ }