Reference¶
- class Client(token, config, isUserProvidedToken=false)¶
exported from
Client
- Arguments:
token (string)
config (ClientConfig)
isUserProvidedToken (boolean)
- Client.getAuthHeader()¶
Returns the authorization header object with the Bearer token.
- Returns:
Record<string, string> – An object containing the Authorization header with the Bearer token
Example
const client = Client.getInstance(); const headers = { 'Content-Type': 'application/json', ...client.getAuthHeader() }; // headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer token...' }
- Client.getClientId()¶
Returns the client ID configured for this client instance.
- Returns:
string – The client ID string
Example
const client = Client.getInstance(); const clientId = client.getClientId(); console.log(clientId); // e.g., 'client123'
- Client.getClientSource()¶
Returns the client source identifier for this client instance.
- Returns:
string – The client source string
Example
const client = Client.getInstance(); const source = client.getClientSource(); console.log(source); // e.g., 'web', 'mobile', 'sdk'
- Client.getDomain()¶
Returns the domain URL configured for the client.
- Returns:
string – The domain URL string
Example
const client = Client.getInstance(); const apiDomain = client.getDomain(); console.log(apiDomain); // e.g., 'https://api.example.com'
- async Client.refreshToken()¶
Refreshes the authentication token if it’s about to expire. Only refreshes if the token was not provided by the user and is expiring within 60 seconds.
- Returns:
Promise<void> – A promise that resolves when the token refresh is complete
Example
const client = Client.getInstance(); // Ensure token is fresh before making API requests await client.refreshToken(); // Proceed with API requests...
- static async Client.getClient(config)¶
Initializes and returns a Client instance with the provided configuration.
- Arguments:
config (ClientConfig) – The client configuration object
- Returns:
Promise<void> – A promise that resolves when the client is initialized
Example
// Initialize with authentication URL await Client.getClient({ host: 'https://api.example.com', authUrl: 'https://auth.example.com', clientId: 'client123', orgId: 'org456' }); // Initialize with existing token await Client.getClient({ token: 'existing-jwt-token', clientId: 'client123' }); // Initialize with API key await Client.getClient({ apiKey: 'your-api-key', clientId: 'client123', orgId: 'org456' });
- static Client.getInstance()¶
Returns the singleton instance of the Client.
- Returns:
Client – The singleton Client instance
Example
// First initialize the client await Client.getClient({ clientId: 'client123', orgId: 'org456' }); // Then get the instance const client = Client.getInstance(); // Use the client...
- async request.makeApiRequest<T>(config)¶
Makes an API request with standardized headers and token refresh.
- Type parameters:
T – The expected response data type
- Arguments:
config (RequestConfig) – The request configuration including method, URL, data, params, and optional headers
- Returns:
Promise<T> – A promise that resolves to the response data of type T
Example
interface UserData { id: string; name: string; email: string; } const userData = await makeApiRequest<UserData>({ method: 'GET', url: 'https://api.example.com/users/123', headers: { 'X-Custom-Header': 'value' } }); console.log(userData.name); // Typed access to the response data
- utils.findExpiryTime(token)¶
Extracts the expiration time from a JWT token and give the token expiry time.
- Arguments:
token (string) – The JWT token to extract the expiration time from
- Returns:
any – The expiration time value from the token’s payload
Example
const jwtToken = 'your-token'; const expiryTime = findExpiryTime(jwtToken); console.log(new Date(expiryTime * 1000).toISOString()); // Convert to date if it's a Unix timestamp
API¶
- async Factor.retrieveFactor(payload)¶
Retrieves a factor details by making a POST request to the factor API endpoint.
- Arguments:
payload (FactorRequest) – The factor request data to be sent to the API
- Returns:
Promise<string> – A promise that resolves to the factor string returned by the API
Example
const factorRequest = { "activity": { "factorId": 12345 } }; or const factorRequest = { "time" : { "date": "2025-01-04" }, "location": { "country": "usa", "stateProvince": "new york" }, "activity": { "type":"natural gas" }, "includeDetails": true }; const factor = await retrieveFactor(factorRequest);
- async Fugitive.calculate(payload)¶
Performs scope 1 fugitve emission calculations by making a POST request to the fugitive API endpoint.
- Arguments:
payload (CommonRequest) – The request data to be sent to the API
- Returns:
Promise<string> – A promise that resolves to the calculation result string returned by the API
Example
const request = { "time": { "date": "2025-01-04" }, "location": { "country": "usa" }, "activity": { "type": "R134A", "value": 150, "unit": "kg" }, "includeDetails": false }; const result = await calculate(request);
- async Calculation.calculate(payload)¶
Performs emission calculations by making a POST request to the generic calculation API endpoint.
- Arguments:
payload (CalculationRequest) – The calculation request data to be sent to the API
- Returns:
Promise<string> – A promise that resolves to the calculation result string returned by the API
Example
const calculationRequest = { "time" : { "date": "2025-01-04" }, "location": { "country": "usa", "stateProvince": "new york" }, "activity": { "type":"Waste Combusted - Steel Cans:Default factor", "unit": "kg", "value": 1223123.121 }, "includeDetails": true }; const result = await calculate(calculationRequest);
- async Location.calculate(payload)¶
Performs scope 2 purchased energy Emission calculations by making a POST request to the location API endpoint.
- Arguments:
payload (LocationRequest) – The location request data to be sent to the API
- Returns:
Promise<string> – A promise that resolves to the purchased energy emission result string returned by the API
Example
const calculationRequest = { "time" : { "date": "2025-01-04" }, "location": { "country": "usa", "stateProvince": "new york" }, "activity": { "type":"electricity", "unit": "kwh", "value": 14123143 }, "includeDetails": true }; const result = await calculate(calculationRequest);
- async Mobile.calculate(payload)¶
Performs Scope 1 mobile emissions related calculations by making a POST request to the mobile API endpoint.
- Arguments:
payload (CommonRequest) – The request data to be sent to the mobile API
- Returns:
Promise<string> – A promise that resolves to the calculation result string returned by the API
Example
const request = { "time": { "date": "2022-01-01" }, "location": { "country": "USA", "stateProvince": "new york" }, "activity": { "type": "Diesel Fuel", "value": 1500000, "unit": "l" }, "includeDetails": false }; const result = await calculate(request);
- async Stationary.calculate(payload)¶
Performs Scope 1 stationary calculations by making a POST request to the stationary API endpoint.
- Arguments:
payload (CommonRequest) – The request data to be sent to the stationary API
- Returns:
Promise<string> – A promise that resolves to the calculation result string returned by the API
Example
const request = { "time" : { "date": "2025-01-04" }, "location": { "country": "usa", "stateProvince": "new york" }, "activity": { "type":"Coal - Lignite", "unit": "KJ", "value": 3 }, "includeDetails": true }; const result = await calculate(request);
- async TransportationAndDistribution.calculate(payload)¶
Performs Scope 2 transportation and distribution emission calculations by making a POST request to the transportation and distribution API endpoint.
- Arguments:
payload (CalculationRequest) – The calculation request data for transportation and distribution operations
- Returns:
Promise<string> – A promise that resolves to the calculation result string returned by the API
Example
const request = { "time" : { "date": "2025-01-04" }, "location": { "country": "usa" }, "activity": { "type": "Freight - Cargo Ship - Bulk Carrier - 0-9999 dwt", "unit": ["t","km"], "value": [1.0,1.0] }, "includeDetails": true }; const result = await calculate(request);
Interfaces¶
- interface Api.LocationRequestWithoutFactorId¶
Request parameters for location-based calculations without specifying a factor ID. Used for calculating emissions or other metrics based on location data.
LocationRequestWithoutFactorId
exported from
interfaces.Api
- interface Api.LocationRequestWithFactorId¶
Request parameters for location-based calculations with a specific factor ID. Used when the emission factor is already known and doesn’t need to be determined by location.
LocationRequestWithFactorId
exported from
interfaces.Api
- interface Api.CommonRequestWithoutFactorId¶
Common request parameters for calculations without specifying a factor ID. Used for general-purpose calculations based on location, time, and activity data.
CommonRequestWithoutFactorId
exported from
interfaces.Api
- interface Api.CommonRequestWithFactorId¶
Common request parameters for calculations with a specific factor ID. Used when the emission factor is already known and doesn’t need to be determined by location.
CommonRequestWithFactorId
exported from
interfaces.Api
- interface Api.GenericCalculationRequestWithoutFactorId¶
Request parameters for generic calculations without specifying a factor ID. Used for calculations that may involve multiple units or complex activity data.
GenericCalculationRequestWithoutFactorId
exported from
interfaces.Api
- interface Api.GenericCalculationRequestWithFactorId¶
Request parameters for generic calculations with a specific factor ID. Used for complex calculations where the emission factor is already known.
GenericCalculationRequestWithFactorId
exported from
interfaces.Api
- interface Api.FactorRequestWithoutFactorId¶
Request parameters for retrieving factor information without specifying a factor ID. Used to look up appropriate emission factors based on location, time, and activity type.
FactorRequestWithoutFactorId
exported from
interfaces.Api
- interface Api.FactorRequestWithFactorId¶
Request parameters for retrieving specific factor information by ID. Used when the exact factor to retrieve is already known.
FactorRequestWithFactorId
exported from
interfaces.Api
- interface Api.SearchRequest¶
Request parameters for searching factors or activities. Used to find relevant factors or activities based on location, time, and search criteria.
SearchRequest
exported from
interfaces.Api
- interface common.Location¶
- Represents a geographical location.
Location
exported from
interfaces.common
- interface common.Time¶
- Represents a point in time with date information.
Time
exported from
interfaces.common
- interface common.Activity¶
- Represents an activity with type, value, and unit of measurement.
Activity
exported from
interfaces.common
- interface common.ActivityWithFactorId¶
- Represents an activity identified by a factor ID instead of a type.
ActivityWithFactorId
exported from
interfaces.common
- interface common.CombinedUnitsActivity¶
- Represents an activity that can have single or multiple values and units.
CombinedUnitsActivity
exported from
interfaces.common
- interface common.CombinedUnitsActivityWithFactorId¶
- Represents an activity with factor ID that can have single or multiple values and units.
CombinedUnitsActivityWithFactorId
exported from
interfaces.common
- interface common.FactorActivity¶
- Represents a factor activity with a type and optional unit.
FactorActivity
exported from
interfaces.common
- interface common.FactorActivityWithFactorId¶
- Represents a factor activity identified by a factor ID instead of a type.
FactorActivityWithFactorId
exported from
interfaces.common
- interface common.SearchActivity¶
- Represents a search query for activities.
SearchActivity
exported from
interfaces.common
- interface common.Pagination¶
- Represents pagination parameters for paginated results.
Pagination
exported from
interfaces.common
- interface Config.RequestConfig¶
- Configuration for making HTTP requests.
RequestConfig
exported from
interfaces.Config
- interface Config.ClientConfig¶
- Configuration for initializing a client instance.
ClientConfig
exported from
interfaces.Config