emissions-api-sdk 1.0.0 → 1.0.1
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.
- package/README.md +33 -2
- package/dist/Constants.js +23 -1
- package/dist/api/Calculation.js +77 -14
- package/dist/api/Factor.js +97 -16
- package/dist/api/Fugitive.js +75 -12
- package/dist/api/Location.js +75 -14
- package/dist/api/Mobile.js +66 -0
- package/dist/api/Stationary.js +77 -14
- package/dist/api/TransportationAndDistribution.js +75 -12
- package/dist/types/Constants.d.ts +22 -0
- package/dist/types/api/Calculation.d.ts +52 -14
- package/dist/types/api/Factor.d.ts +64 -16
- package/dist/types/api/Fugitive.d.ts +50 -12
- package/dist/types/api/Location.d.ts +50 -14
- package/dist/types/api/Mobile.d.ts +41 -0
- package/dist/types/api/Stationary.d.ts +52 -14
- package/dist/types/api/TransportationAndDistribution.d.ts +50 -12
- package/docs/.nojekyll +0 -0
- package/docs/_sources/authentication.rst.txt +164 -0
- package/docs/_sources/client.rst.txt +137 -0
- package/docs/_sources/getting_started.rst.txt +164 -0
- package/docs/_sources/index.rst.txt +29 -0
- package/docs/_sources/reference.rst.txt +129 -0
- package/docs/_sources/sdk.rst.txt +13 -0
- package/docs/_static/basic.css +906 -0
- package/docs/_static/custom.css +16 -0
- package/docs/_static/debug.css +69 -0
- package/docs/_static/doctools.js +149 -0
- package/docs/_static/documentation_options.js +13 -0
- package/docs/_static/file.png +0 -0
- package/docs/_static/language_data.js +192 -0
- package/docs/_static/minus.png +0 -0
- package/docs/_static/plus.png +0 -0
- package/docs/_static/pygments.css +250 -0
- package/docs/_static/scripts/furo-extensions.js +0 -0
- package/docs/_static/scripts/furo.js +3 -0
- package/docs/_static/scripts/furo.js.LICENSE.txt +7 -0
- package/docs/_static/scripts/furo.js.map +1 -0
- package/docs/_static/searchtools.js +635 -0
- package/docs/_static/skeleton.css +296 -0
- package/docs/_static/sphinx_highlight.js +154 -0
- package/docs/_static/sphinx_js.css +0 -0
- package/docs/_static/styles/furo-extensions.css +2 -0
- package/docs/_static/styles/furo-extensions.css.map +1 -0
- package/docs/_static/styles/furo.css +2 -0
- package/docs/_static/styles/furo.css.map +1 -0
- package/docs/authentication.html +513 -0
- package/docs/client.html +467 -0
- package/docs/genindex.html +482 -0
- package/docs/getting_started.html +481 -0
- package/docs/index.html +334 -0
- package/docs/objects.inv +0 -0
- package/docs/reference.html +1568 -0
- package/docs/sdk.html +324 -0
- package/docs/search.html +287 -0
- package/docs/searchindex.js +1 -0
- package/package.json +1 -1
- package/sphinx-build/Makefile +49 -0
- package/sphinx-build/source/_static/custom.css +16 -0
- package/sphinx-build/source/authentication.rst +164 -0
- package/sphinx-build/source/client.rst +137 -0
- package/sphinx-build/source/conf.py +56 -0
- package/sphinx-build/source/getting_started.rst +164 -0
- package/sphinx-build/source/index.rst +29 -0
- package/sphinx-build/source/reference.rst +129 -0
- package/sphinx-build/source/sdk.rst +13 -0
- package/src/Constants.ts +26 -0
- package/src/api/Calculation.ts +84 -15
- package/src/api/Factor.ts +108 -19
- package/src/api/Fugitive.ts +83 -13
- package/src/api/Location.ts +84 -15
- package/src/api/Mobile.ts +75 -1
- package/src/api/Stationary.ts +85 -15
- package/src/api/TransportationAndDistribution.ts +84 -13
- package/test/apiTest.test.ts +172 -5
- package/dist/api/Factors.js +0 -98
- package/dist/api/FugitiveEmission.js +0 -40
- package/dist/api/GenericCalculation.js +0 -41
- package/dist/api/LocationEmission.js +0 -41
- package/dist/api/MobileEmission.js +0 -41
- package/dist/api/StationaryEmission.js +0 -41
- package/dist/api/TransportationDistributionEmission.js +0 -40
- package/dist/coverage/clover.xml +0 -225
- package/dist/coverage/coverage-final.json +0 -19
- package/dist/coverage/lcov-report/base.css +0 -224
- package/dist/coverage/lcov-report/block-navigation.js +0 -87
- package/dist/coverage/lcov-report/favicon.png +0 -0
- package/dist/coverage/lcov-report/index.html +0 -161
- package/dist/coverage/lcov-report/prettify.css +0 -1
- package/dist/coverage/lcov-report/prettify.js +0 -2
- package/dist/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/dist/coverage/lcov-report/sorter.js +0 -210
- package/dist/coverage/lcov-report/src/Client.ts.html +0 -838
- package/dist/coverage/lcov-report/src/Constants.ts.html +0 -130
- package/dist/coverage/lcov-report/src/api/Calculation.ts.html +0 -217
- package/dist/coverage/lcov-report/src/api/Factor.ts.html +0 -343
- package/dist/coverage/lcov-report/src/api/FactorSets.ts.html +0 -154
- package/dist/coverage/lcov-report/src/api/Factors.ts.html +0 -403
- package/dist/coverage/lcov-report/src/api/Fugitive.ts.html +0 -214
- package/dist/coverage/lcov-report/src/api/FugitiveEmission.ts.html +0 -214
- package/dist/coverage/lcov-report/src/api/GenericCalculation.ts.html +0 -217
- package/dist/coverage/lcov-report/src/api/Location.ts.html +0 -214
- package/dist/coverage/lcov-report/src/api/LocationEmission.ts.html +0 -214
- package/dist/coverage/lcov-report/src/api/Mobile.ts.html +0 -220
- package/dist/coverage/lcov-report/src/api/MobileEmission.ts.html +0 -220
- package/dist/coverage/lcov-report/src/api/Stationary.ts.html +0 -220
- package/dist/coverage/lcov-report/src/api/StationaryEmission.ts.html +0 -220
- package/dist/coverage/lcov-report/src/api/TransportationAndDistribution.ts.html +0 -211
- package/dist/coverage/lcov-report/src/api/TransportationDistributionEmission.ts.html +0 -211
- package/dist/coverage/lcov-report/src/api/index.html +0 -221
- package/dist/coverage/lcov-report/src/index.html +0 -161
- package/dist/coverage/lcov-report/src/request.ts.html +0 -235
- package/dist/coverage/lcov-report/src/utils.ts.html +0 -139
- package/dist/coverage/lcov-report/test/index.html +0 -116
- package/dist/coverage/lcov-report/test/mocks/CommonRequest.ts.html +0 -142
- package/dist/coverage/lcov-report/test/mocks/FactorRequest.ts.html +0 -133
- package/dist/coverage/lcov-report/test/mocks/GenericCalculationRequest.ts.html +0 -142
- package/dist/coverage/lcov-report/test/mocks/LocationRequest.ts.html +0 -139
- package/dist/coverage/lcov-report/test/mocks/SearchRequest.ts.html +0 -142
- package/dist/coverage/lcov-report/test/mocks/index.html +0 -176
- package/dist/coverage/lcov-report/test/testUtils.ts.html +0 -97
- package/dist/coverage/lcov.info +0 -404
- package/dist/types/api/Factors.d.ts +0 -68
- package/dist/types/api/FugitiveEmission.d.ts +0 -27
- package/dist/types/api/GenericCalculation.d.ts +0 -28
- package/dist/types/api/LocationEmission.d.ts +0 -28
- package/dist/types/api/MobileEmission.d.ts +0 -28
- package/dist/types/api/StationaryEmission.d.ts +0 -28
- package/dist/types/api/TransportationDistributionEmission.d.ts +0 -27
|
@@ -9,20 +9,58 @@ import { CommonRequest } from "../interfaces/Api";
|
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* const request = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
12
|
+
"time" : {
|
|
13
|
+
"date": "2025-01-04"
|
|
14
|
+
},
|
|
15
|
+
"location": {
|
|
16
|
+
"country": "usa",
|
|
17
|
+
"stateProvince": "new york"
|
|
18
|
+
},
|
|
19
|
+
"activity": {
|
|
20
|
+
"type":"Coal - Lignite",
|
|
21
|
+
"unit": "KJ",
|
|
22
|
+
"value": 3
|
|
23
|
+
},
|
|
24
|
+
"includeDetails": true
|
|
25
|
+
};
|
|
26
26
|
* const result = await calculate(request);
|
|
27
27
|
*/
|
|
28
28
|
export declare function calculate(payload: CommonRequest): Promise<string>;
|
|
29
|
+
/**
|
|
30
|
+
* Retrieves available stationary emission calculation types by making a GET request to the API endpoint.
|
|
31
|
+
*
|
|
32
|
+
* @export
|
|
33
|
+
* @return {Promise<string>} A promise that resolves to a string containing the available stationary emission types
|
|
34
|
+
* @throws {Error} May throw an error if the API request fails
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* const types = await getTypes();
|
|
38
|
+
*/
|
|
39
|
+
export declare function getTypes(): Promise<string>;
|
|
40
|
+
/**
|
|
41
|
+
* Retrieves information about geographical areas supported by the stationary emissions API.
|
|
42
|
+
*
|
|
43
|
+
* @export
|
|
44
|
+
* @return {Promise<string>} A promise that resolves to a string containing the supported geographical areas
|
|
45
|
+
* @throws {Error} May throw an error if the API request fails
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* const areas = await getArea();
|
|
49
|
+
*/
|
|
50
|
+
export declare function getArea(): Promise<string>;
|
|
51
|
+
/**
|
|
52
|
+
* Retrieves available units for a specific stationary emission type.
|
|
53
|
+
*
|
|
54
|
+
* @export
|
|
55
|
+
* @param {string} type - The stationary emission type to get units for (e.g., "Coal - Lignite")
|
|
56
|
+
* @return {Promise<string>} A promise that resolves to a string containing the available units
|
|
57
|
+
* @throws {Error} May throw an error if the API request fails
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* Get units for the "Petroleum Based Greases - NC:A" emission stationary type with subtype
|
|
61
|
+
* const units = await getUnits("Petroleum Based Greases - NC:A");
|
|
62
|
+
|
|
63
|
+
* Get units for the "Jet Kerosene" emission stationary type without subtype
|
|
64
|
+
* const units = await getUnits("Jet Kerosene");
|
|
65
|
+
*/
|
|
66
|
+
export declare function getUnits(type: string): Promise<string>;
|
|
@@ -9,19 +9,57 @@ import { CalculationRequest } from "../interfaces/Api";
|
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* const request = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
"time" : {
|
|
13
|
+
"date": "2025-01-04"
|
|
14
|
+
},
|
|
15
|
+
"location": {
|
|
16
|
+
"country": "usa"
|
|
17
|
+
},
|
|
18
|
+
"activity": {
|
|
19
|
+
"type": "Freight - Cargo Ship - Bulk Carrier - 0-9999 dwt",
|
|
20
|
+
"unit": ["t","km"],
|
|
21
|
+
"value": [1.0,1.0]
|
|
22
|
+
},
|
|
23
|
+
"includeDetails": true
|
|
24
24
|
};
|
|
25
25
|
* const result = await calculate(request);
|
|
26
26
|
*/
|
|
27
27
|
export declare function calculate(payload: CalculationRequest): Promise<string>;
|
|
28
|
+
/**
|
|
29
|
+
* Retrieves available transportation and distribution calculation types by making a GET request to the API endpoint.
|
|
30
|
+
*
|
|
31
|
+
* @export
|
|
32
|
+
* @return {Promise<string>} A promise that resolves to a string containing the available transportation and distribution calculation types
|
|
33
|
+
* @throws {Error} May throw an error if the API request fails
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const types = await getTypes();
|
|
37
|
+
*/
|
|
38
|
+
export declare function getTypes(): Promise<string>;
|
|
39
|
+
/**
|
|
40
|
+
* Retrieves information about geographical areas supported by the transportation and distribution API.
|
|
41
|
+
*
|
|
42
|
+
* @export
|
|
43
|
+
* @return {Promise<string>} A promise that resolves to a string containing the supported geographical areas
|
|
44
|
+
* @throws {Error} May throw an error if the API request fails
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* const areas = await getArea();
|
|
48
|
+
*/
|
|
49
|
+
export declare function getArea(): Promise<string>;
|
|
50
|
+
/**
|
|
51
|
+
* Retrieves available units for a specific transportation and distribution calculation type.
|
|
52
|
+
*
|
|
53
|
+
* @export
|
|
54
|
+
* @param {string} type - The transportation and distribution calculation type to get units for (e.g., "Freight - Cargo Ship")
|
|
55
|
+
* @return {Promise<string>} A promise that resolves to a string containing the available units
|
|
56
|
+
* @throws {Error} May throw an error if the API request fails
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // Get units for the "Business Travel - Cars:Diesel - Small" transportation and distribution type without subtype
|
|
60
|
+
* const units = await getUnits("Business Travel - Cars:Diesel - Small");
|
|
61
|
+
*
|
|
62
|
+
* // Get units for the "Business Travel - Cars" transportation and distribution type with subtype
|
|
63
|
+
* const units = await getUnits("Business Travel - Cars");
|
|
64
|
+
*/
|
|
65
|
+
export declare function getUnits(type: string): Promise<string>;
|
package/docs/.nojekyll
ADDED
|
File without changes
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
==============
|
|
2
|
+
Authentication
|
|
3
|
+
==============
|
|
4
|
+
|
|
5
|
+
Overview
|
|
6
|
+
--------
|
|
7
|
+
|
|
8
|
+
The IBM Envizi - Emissions API Node.js SDK uses OAuth 2.0 Bearer Tokens for authentication. This page explains the authentication process, options, and security best practices in detail.
|
|
9
|
+
|
|
10
|
+
Authentication Methods
|
|
11
|
+
----------------------
|
|
12
|
+
|
|
13
|
+
The SDK provides two authentication methods:
|
|
14
|
+
|
|
15
|
+
API Key Authentication (Recommended)
|
|
16
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
17
|
+
|
|
18
|
+
This method automatically handles token generation and refreshing:
|
|
19
|
+
|
|
20
|
+
.. code-block:: javascript
|
|
21
|
+
|
|
22
|
+
import { Client } from 'emissions-api-sdk';
|
|
23
|
+
|
|
24
|
+
await Client.getClient({
|
|
25
|
+
apiKey: process.env.ENVIZI_API_KEY,
|
|
26
|
+
clientId: process.env.ENVIZI_CLIENT_ID,
|
|
27
|
+
orgId: process.env.ENVIZI_ORG_ID
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
**Required parameters:**
|
|
31
|
+
- ``apiKey``: Your API key for authentication
|
|
32
|
+
- ``clientId``: Your client identifier
|
|
33
|
+
- ``orgId``: Your organization identifier
|
|
34
|
+
|
|
35
|
+
Token retrieval
|
|
36
|
+
^^^^^^^^^^^^^^^
|
|
37
|
+
When you call ``Client.getClient()`` with ``apiKey``:
|
|
38
|
+
|
|
39
|
+
- **Method**: ``GET`` to the token endpoint (default: ``https://api.ibm.com/saascore/run/authentication-retrieve/api-key``)
|
|
40
|
+
- **Headers**:
|
|
41
|
+
- ``X-Api-Key: <apiKey>``
|
|
42
|
+
- ``X-IBM-Client-Id: saascore-<clientId>``
|
|
43
|
+
- ``Accept: application/json``
|
|
44
|
+
- **Query param**: ``orgId=<orgId>``
|
|
45
|
+
- **Response**: a bearer token (string) which the SDK trims, caches, and uses for API calls.
|
|
46
|
+
|
|
47
|
+
Pre-generated Token Authentication
|
|
48
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
49
|
+
|
|
50
|
+
For scenarios where token generation is handled externally:
|
|
51
|
+
|
|
52
|
+
.. code-block:: javascript
|
|
53
|
+
|
|
54
|
+
import { Client } from 'emissions-api-sdk';
|
|
55
|
+
|
|
56
|
+
await Client.getClient({
|
|
57
|
+
token: process.env.JWT_TOKEN,
|
|
58
|
+
clientId: process.env.ENVIZI_CLIENT_ID
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
**Required parameters:**
|
|
62
|
+
- ``token``: A pre-generated authentication token
|
|
63
|
+
- ``clientId``: Your client identifier
|
|
64
|
+
|
|
65
|
+
Token lifetime & refresh
|
|
66
|
+
^^^^^^^^^^^^^^^^^^^^^^^^
|
|
67
|
+
- The SDK decodes the JWT’s ``exp`` and stores it.
|
|
68
|
+
- In user-provided token mode, no refresh occurs; re-initialize with a fresh token when needed.
|
|
69
|
+
|
|
70
|
+
Authentication Flow
|
|
71
|
+
-------------------
|
|
72
|
+
|
|
73
|
+
The authentication process follows these steps:
|
|
74
|
+
|
|
75
|
+
1. **Initialization**:
|
|
76
|
+
- User provides authentication credentials
|
|
77
|
+
- Client validates the credentials format
|
|
78
|
+
|
|
79
|
+
2. **Token Generation** (API Key method):
|
|
80
|
+
- Client sends API key to authentication service
|
|
81
|
+
- Authentication service validates credentials
|
|
82
|
+
- Authentication service returns a token with expiration time
|
|
83
|
+
- Client stores the token securely
|
|
84
|
+
|
|
85
|
+
3. **Token Validation**:
|
|
86
|
+
- Before each API request, token validity is checked
|
|
87
|
+
- If valid, the token is included in the request
|
|
88
|
+
- If expired or near expiration, token is refreshed
|
|
89
|
+
|
|
90
|
+
4. **Token Refreshing**:
|
|
91
|
+
- Client detects when token is approaching expiration
|
|
92
|
+
- Client automatically requests a new token
|
|
93
|
+
- New token replaces the old one for future requests
|
|
94
|
+
|
|
95
|
+
Authentication Headers
|
|
96
|
+
----------------------
|
|
97
|
+
|
|
98
|
+
The SDK automatically includes the necessary headers for authentication:
|
|
99
|
+
|
|
100
|
+
For token generation:
|
|
101
|
+
- ``X-Api-Key``: Your API key
|
|
102
|
+
- ``X-IBM-Client-Id``: saascore-{your-client-id}
|
|
103
|
+
|
|
104
|
+
For API requests:
|
|
105
|
+
- ``Authorization``: Bearer {token}
|
|
106
|
+
- ``X-IBM-Client-Id``: ghgemissions-{your-client-id}
|
|
107
|
+
- ``X-Client-Source``: node-sdk
|
|
108
|
+
|
|
109
|
+
Architecture Diagram
|
|
110
|
+
--------------------
|
|
111
|
+
|
|
112
|
+
::
|
|
113
|
+
|
|
114
|
+
+-----------------+ +-------------------------+
|
|
115
|
+
| Your application| | Token Service (GET) |
|
|
116
|
+
| (init once) | | authUrl / default URL |
|
|
117
|
+
+--------+--------+ +-----------+-------------+
|
|
118
|
+
| ^
|
|
119
|
+
| getClient({apiKey, clientId, | (managed mode only:
|
|
120
|
+
| orgId, [authUrl], [host]}) | X-Api-Key, saascore-Client-Id,
|
|
121
|
+
v | orgId => returns JWT)
|
|
122
|
+
+--------+--------------------------------+-------------+
|
|
123
|
+
| Client (singleton) |
|
|
124
|
+
| - token, exp, domain, clientId |
|
|
125
|
+
| - refresh before expiry (managed mode) |
|
|
126
|
+
+--------+--------------------------------+-------------+
|
|
127
|
+
| ^
|
|
128
|
+
| makeApiRequest(...) |
|
|
129
|
+
v |
|
|
130
|
+
+--------+--------------------------------+-------------+
|
|
131
|
+
| Request layer (axios) |
|
|
132
|
+
| Adds headers: |
|
|
133
|
+
| - Authorization: Bearer <token> |
|
|
134
|
+
| - X-IBM-Client-Id: ghgemissions-<clientId> |
|
|
135
|
+
| - X-Client-Source: excel | node-sdk |
|
|
136
|
+
+--------+--------------------------------+-------------+
|
|
137
|
+
|
|
|
138
|
+
v
|
|
139
|
+
+--------+--------------------------------+-------------+
|
|
140
|
+
| Emissions API (host/domain) |
|
|
141
|
+
+-------------------------------------------------------+
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
Security Best Practices
|
|
145
|
+
-----------------------
|
|
146
|
+
|
|
147
|
+
1. **Use environment variables** for credentials:
|
|
148
|
+
|
|
149
|
+
.. code-block:: javascript
|
|
150
|
+
|
|
151
|
+
await Client.getClient({
|
|
152
|
+
apiKey: process.env.ENVIZI_API_KEY,
|
|
153
|
+
clientId: process.env.ENVIZI_CLIENT_ID,
|
|
154
|
+
orgId: process.env.ENVIZI_ORG_ID
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
2. **Never hardcode** credentials in your application code
|
|
158
|
+
3. **Implement proper access controls** for API keys
|
|
159
|
+
4. **Use secure environment variables** for production deployments
|
|
160
|
+
5. **Implement least privilege** principles for API access
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
For more information about the Client that manages authentication, see the :doc:`client` page.
|
|
164
|
+
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
======
|
|
2
|
+
Client
|
|
3
|
+
======
|
|
4
|
+
|
|
5
|
+
Overview
|
|
6
|
+
--------
|
|
7
|
+
|
|
8
|
+
The Client is the central component of the IBM Envizi - Emissions API Node.js SDK. It handles authentication, maintains connections to the API, and provides a unified interface for all API operations.
|
|
9
|
+
|
|
10
|
+
.. code-block:: none
|
|
11
|
+
|
|
12
|
+
+----------------------+
|
|
13
|
+
| Application |<-----------+
|
|
14
|
+
+----------------------+ |
|
|
15
|
+
| |
|
|
16
|
+
v |
|
|
17
|
+
+----------------------+ |
|
|
18
|
+
| Client |<-----------+
|
|
19
|
+
+----------------------+ |
|
|
20
|
+
| |
|
|
21
|
+
v |
|
|
22
|
+
+----------------------+ +----------------------+
|
|
23
|
+
| Authentication |---->| API Module Requests |
|
|
24
|
+
+----------------------+ +----------------------+
|
|
25
|
+
|
|
26
|
+
Client Initialization
|
|
27
|
+
---------------------
|
|
28
|
+
|
|
29
|
+
The Client is designed as a singleton to ensure consistent authentication across your application. Initialize it once at application startup:
|
|
30
|
+
|
|
31
|
+
1. Managed token (SDK fetches & refreshes)
|
|
32
|
+
|
|
33
|
+
.. code-block:: javascript
|
|
34
|
+
|
|
35
|
+
import { Client } from 'emissions-api-sdk';
|
|
36
|
+
|
|
37
|
+
await Client.getClient({
|
|
38
|
+
apiKey: process.env.ENVIZI_API_KEY,
|
|
39
|
+
clientId: process.env.ENVIZI_CLIENT_ID,
|
|
40
|
+
orgId: process.env.ENVIZI_ORG_ID
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
2. User-provided token (SDK will **not** refresh it)
|
|
44
|
+
|
|
45
|
+
.. code-block:: javascript
|
|
46
|
+
|
|
47
|
+
import { Client } from "emissions-api-sdk";
|
|
48
|
+
|
|
49
|
+
await Client.getClient({
|
|
50
|
+
clientId: process.env.ENVIZI_CLIENT_ID,
|
|
51
|
+
token: process.env.TOKEN
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
Configuration Options
|
|
55
|
+
---------------------
|
|
56
|
+
|
|
57
|
+
The Client accepts the following configuration parameters:
|
|
58
|
+
|
|
59
|
+
.. list-table::
|
|
60
|
+
:header-rows: 1
|
|
61
|
+
:widths: 20 15 65
|
|
62
|
+
|
|
63
|
+
* - Parameter
|
|
64
|
+
- Required
|
|
65
|
+
- Description
|
|
66
|
+
* - apiKey
|
|
67
|
+
- Yes*
|
|
68
|
+
- API key for authentication (required unless using token)
|
|
69
|
+
* - clientId
|
|
70
|
+
- Yes
|
|
71
|
+
- Client ID for API access
|
|
72
|
+
* - orgId
|
|
73
|
+
- Yes*
|
|
74
|
+
- Organization ID (required with apiKey)
|
|
75
|
+
* - token
|
|
76
|
+
- Yes*
|
|
77
|
+
- Pre-generated token (required if not using apiKey)
|
|
78
|
+
* - host
|
|
79
|
+
- No
|
|
80
|
+
- Custom API endpoint (defaults to production)
|
|
81
|
+
* - authUrl
|
|
82
|
+
- No
|
|
83
|
+
- Custom authentication endpoint (defaults to production)
|
|
84
|
+
|
|
85
|
+
.. note::
|
|
86
|
+
Either ``apiKey+orgId`` OR ``token`` is required
|
|
87
|
+
|
|
88
|
+
Client Responsibilities
|
|
89
|
+
-----------------------
|
|
90
|
+
|
|
91
|
+
The Client handles several key responsibilities:
|
|
92
|
+
|
|
93
|
+
1. **Singleton Management**: Ensures only one Client instance exists
|
|
94
|
+
2. **Configuration Validation**: Verifies required parameters
|
|
95
|
+
3. **Token Generation**: Obtains tokens from the authentication service
|
|
96
|
+
4. **Token Refreshing**: Automatically refreshes tokens before expiration
|
|
97
|
+
5. **Request Authentication**: Adds proper headers to all API requests
|
|
98
|
+
|
|
99
|
+
Error Handling
|
|
100
|
+
--------------
|
|
101
|
+
|
|
102
|
+
Handle authentication errors gracefully:
|
|
103
|
+
|
|
104
|
+
.. code-block:: javascript
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
await Client.getClient({
|
|
108
|
+
apiKey: process.env.ENVIZI_API_KEY,
|
|
109
|
+
clientId: process.env.ENVIZI_CLIENT_ID,
|
|
110
|
+
orgId: process.env.ENVIZI_ORG_ID
|
|
111
|
+
});
|
|
112
|
+
} catch (error) {
|
|
113
|
+
console.error("Authentication error:", error.message);
|
|
114
|
+
// Implement appropriate error handling
|
|
115
|
+
if (error.response) {
|
|
116
|
+
// Handle specific HTTP error responses
|
|
117
|
+
console.error("Status:", error.response.status);
|
|
118
|
+
console.error("Details:", error.response.data);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
Best Practices
|
|
124
|
+
--------------
|
|
125
|
+
|
|
126
|
+
For optimal Client usage:
|
|
127
|
+
|
|
128
|
+
1. **Initialize once** at application startup
|
|
129
|
+
2. **Store credentials securely** using environment variables
|
|
130
|
+
3. **Implement proper error handling** for authentication failures
|
|
131
|
+
4. **Let the SDK handle** token refreshing automatically
|
|
132
|
+
5. **Reuse the Client instance** across your application
|
|
133
|
+
|
|
134
|
+
Authentication Integration
|
|
135
|
+
--------------------------
|
|
136
|
+
|
|
137
|
+
The Client works closely with the Authentication module to manage tokens. For detailed information about the authentication process, supported methods, and security best practices, see the :doc:`authentication` page.
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
===============
|
|
2
|
+
Getting Started
|
|
3
|
+
===============
|
|
4
|
+
|
|
5
|
+
Prerequisites
|
|
6
|
+
-------------
|
|
7
|
+
|
|
8
|
+
Before using the SDK, ensure you have:
|
|
9
|
+
|
|
10
|
+
- **Node.js** installed
|
|
11
|
+
- Active internet connection
|
|
12
|
+
- Sign up for the preview waitlist `IBMid sign up <https://www.ibm.com/account/reg/us-en/signup?formid=urx-53659>`_ page.
|
|
13
|
+
- API credentials (``apiKey``, ``tenantId``, ``orgId``), are available on the Emissions API `Overview Page <https://www-dev.supply-chain.ibm.com/envizi/emissions-api-home/overview?cuiURL=%2Femissions-api-home%2Foverview>`_ after sign up
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
Installation
|
|
17
|
+
------------
|
|
18
|
+
|
|
19
|
+
You can install the SDK using `npm <https://www.npmjs.com/package/emissions-api-sdk>`_ or `yarn <https://yarnpkg.com/package?q=emissions-api-sdk&name=emissions-api-sdk>`_:
|
|
20
|
+
|
|
21
|
+
Using npm:
|
|
22
|
+
|
|
23
|
+
.. code-block:: bash
|
|
24
|
+
|
|
25
|
+
npm install emissions-api-sdk
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
Using yarn:
|
|
29
|
+
|
|
30
|
+
.. code-block:: bash
|
|
31
|
+
|
|
32
|
+
yarn install emissions-api-sdk
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
Basic Import
|
|
36
|
+
~~~~~~~~~~~~
|
|
37
|
+
|
|
38
|
+
After installation, you can import the SDK in your project:
|
|
39
|
+
|
|
40
|
+
.. code-block:: javascript
|
|
41
|
+
|
|
42
|
+
// Import the entire SDK
|
|
43
|
+
const enviziSDK = require('emissions-api-sdk');
|
|
44
|
+
|
|
45
|
+
// Or using ES modules
|
|
46
|
+
import { Client, LocationEmission } from 'emissions-api-sdk';
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
Authentication
|
|
50
|
+
--------------
|
|
51
|
+
|
|
52
|
+
Initialize the client with your API credentials:
|
|
53
|
+
|
|
54
|
+
The SDK provides two authentication methods:
|
|
55
|
+
|
|
56
|
+
- Using an **API key** with following required Headers:
|
|
57
|
+
- ``X-Api-Key``: Your API key
|
|
58
|
+
- ``X-IBM-Client-Id``: saascore-{your-client-id}
|
|
59
|
+
- ``accept``: application/json
|
|
60
|
+
|
|
61
|
+
- Using a **Pre-generated token**
|
|
62
|
+
- Token Generation Endpoint: https://api.ibm.com/saascore/run/authentication-retrieve/api-key
|
|
63
|
+
|
|
64
|
+
Using API Key (Recommended)
|
|
65
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
66
|
+
|
|
67
|
+
.. code-block:: javascript
|
|
68
|
+
|
|
69
|
+
import { Client } from 'emissions-api-sdk';
|
|
70
|
+
|
|
71
|
+
async function initialize() {
|
|
72
|
+
try {
|
|
73
|
+
await Client.getClient({
|
|
74
|
+
apiKey: "your-api-key",
|
|
75
|
+
clientId: "your-client-id",
|
|
76
|
+
orgId: "your-org-id"
|
|
77
|
+
});
|
|
78
|
+
console.log("Client initialized successfully");
|
|
79
|
+
} catch (error) {
|
|
80
|
+
console.error("Failed to initialize client:", error);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
initialize();
|
|
85
|
+
|
|
86
|
+
Using Pre-generated Token
|
|
87
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
88
|
+
|
|
89
|
+
If you already have a token, you can use it directly:
|
|
90
|
+
|
|
91
|
+
.. code-block:: javascript
|
|
92
|
+
|
|
93
|
+
import { Client } from 'emissions-api-sdk';
|
|
94
|
+
|
|
95
|
+
async function initialize() {
|
|
96
|
+
try {
|
|
97
|
+
await Client.getClient({
|
|
98
|
+
token: "your-pre-generated-token",
|
|
99
|
+
clientId: "your-client-id"
|
|
100
|
+
});
|
|
101
|
+
console.log("Client initialized successfully");
|
|
102
|
+
} catch (error) {
|
|
103
|
+
console.error("Failed to initialize client:", error);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
initialize();
|
|
108
|
+
|
|
109
|
+
First API Call
|
|
110
|
+
--------------
|
|
111
|
+
|
|
112
|
+
After initializing the client, you can make your first API call. Here's an example of calculating location-based emissions:
|
|
113
|
+
|
|
114
|
+
.. code-block:: javascript
|
|
115
|
+
|
|
116
|
+
import { Client, LocationEmission } from 'emissions-api-sdk';
|
|
117
|
+
|
|
118
|
+
async function calculateEmissions() {
|
|
119
|
+
try {
|
|
120
|
+
// Initialize client
|
|
121
|
+
await Client.getClient({
|
|
122
|
+
apiKey: "your-api-key",
|
|
123
|
+
clientId: "your-client-id",
|
|
124
|
+
orgId: "your-org-id"
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// Make API call
|
|
128
|
+
const result = await LocationEmission.calculate({
|
|
129
|
+
"location": {
|
|
130
|
+
"country": "USA",
|
|
131
|
+
"stateProvince": "california"
|
|
132
|
+
},
|
|
133
|
+
"activity": {
|
|
134
|
+
"type": "electricity",
|
|
135
|
+
"value": 1,
|
|
136
|
+
"unit": "kWh"
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
console.log("Emission calculation result:", result);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
console.error("Error calculating emissions:", error);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
calculateEmissions();
|
|
147
|
+
|
|
148
|
+
Example Response
|
|
149
|
+
----------------
|
|
150
|
+
|
|
151
|
+
The API returns emission calculation results in JSON format. Here's an example response:
|
|
152
|
+
|
|
153
|
+
.. code-block:: json
|
|
154
|
+
|
|
155
|
+
{
|
|
156
|
+
"transactionId": "95a7efe7-02ae-47a3-a7fd-831bfca7cecd",
|
|
157
|
+
"totalCO2e": 0.20750174,
|
|
158
|
+
"CO2": 0.20681091,
|
|
159
|
+
"CH4": 0.00033022,
|
|
160
|
+
"N2O": 0.00036061,
|
|
161
|
+
"indirectCO2e": 0.01115131,
|
|
162
|
+
"unit": "kgCO2e",
|
|
163
|
+
"description": "The electricity emissions factor used to calculate this result was obtained from the year 2022 Managed - eGRID & US Climate Leaders factor set for the area United States and the region California."
|
|
164
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
IBM Envizi - Emissions API Node.js SDK
|
|
2
|
+
======================================
|
|
3
|
+
|
|
4
|
+
.. toctree::
|
|
5
|
+
:maxdepth: 2
|
|
6
|
+
:caption: Contents:
|
|
7
|
+
|
|
8
|
+
getting_started
|
|
9
|
+
sdk
|
|
10
|
+
reference
|
|
11
|
+
|
|
12
|
+
Introduction
|
|
13
|
+
============
|
|
14
|
+
|
|
15
|
+
IBM Envizi - Emissions API (Emissions API) is a managed factor database and calculation engine for embedding greenhouse gas (GHG) emissions calculations into operational decision making.
|
|
16
|
+
|
|
17
|
+
The **emissions-api-sdk** is a Node.js SDK for using Emissions API in your projects.
|
|
18
|
+
|
|
19
|
+
Supported Emission Types
|
|
20
|
+
------------------------
|
|
21
|
+
|
|
22
|
+
* **Location** – emissions from region-specific factors
|
|
23
|
+
* **Fugitive** – emissions from leaks or fugitive sources
|
|
24
|
+
* **Mobile** – emissions from vehicles and mobile equipment
|
|
25
|
+
* **Stationary** – emissions from fixed fuel consumption
|
|
26
|
+
* **Transportation & distribution** – logistics and supply chain activities
|
|
27
|
+
* **Generic** – custom emission calculations
|
|
28
|
+
|
|
29
|
+
The SDK is designed for embedding emission calculations in applications, building sustainability dashboards, automating large-scale datasets, and tracking carbon footprints.
|