TypeScript
The TypeScript SDK enables seamless integration with Optum Connections APIs for JavaScript and TypeScript applications. It provides strongly-typed models, easy configuration, and asynchronous methods to simplify API interactions. Below is a guide to help you get started with the SDK.
Installation
- Run the following command to install the SDK from the private registry:
npm install @optum-financial/sdk
Prerequisites
-
Environment variables:
- Ensure the following environment variables are set:
ENVIROMENT: The environment to use (prodornonprod).API_KEY: Your API key for authentication.
- Ensure the following environment variables are set:
-
Node.js version:
- Ensure you are using Node.js version 14 or higher.
Configuration
-
Set up environment variables:
- Use a
.envfile to configure the SDK:ENVIRONMENT=nonprod API_KEY=your-api-key
- Use a
-
Load configuration:
- Use the
dotenvpackage to load environment variables:import * as dotenv from 'dotenv'; dotenv.config();
- Use the
-
Validate configuration:
- Ensure all required configuration values are present:
const getConfigurationValue = (envVariable: string, configName: string): string => { const value = process.env[envVariable]; if (!value) { console.error(`Error: Missing configuration value for '${configName}'.`); process.exit(1); } return value; }; const environment = getConfigurationValue('ENVIRONMENT', 'Environment'); const apiKey = getConfigurationValue('API_KEY', 'API Key');
- Ensure all required configuration values are present:
Usage
Here’s an example of how to use the SDK to interact with various APIs:
import { v1 } from "@optum-financial/sdk";
import * as dotenv from 'dotenv';
dotenv.config();
const runExample = async () => {
// Configuration
const environment = getConfigurationValue("ENVIRONMENT", "Environment", "nonprod");
const apiKey = getConfigurationValue('API_KEY', 'API Key', 'nonprod');
if (environment !== "nonprod" && environment !== "prod") {
// error
}
if (!environment || !apiKey) {
console.error("Error: Missing environment or API key configuration.");
return;
}
console.log(`Environment: ${environment}, API Key: ${apiKey.replace(apiKey.substring(4), "[...]")}`);
// Initialize the SDK
const { paymentsApi, eventsApi, accountsApi } = v1({
apiKey,
env: environment as "prod" | "nonprod",
});
// Example: Fax API
try {
console.log("\nTesting Fax API...");
const faxId = "856bdd48-e709-bee7-619d-caa40757c7f2"; // Replace with your fax ID
const faxDetails = await paymentsApi.getFax(faxId);
console.log("Fax Details:", faxDetails);
} catch (error) {
console.error("Error calling Fax API:", error);
}
};
await runExample();Explanation of the code
1. Configuration:
- The SDK is configured using environment variables (
ENVIROMENTandAPI_KEY).\
2. Initialize the SDK:
- The
v1function initializes the SDK with the provided environment and API key.
3. Fax API example:
- Demonstrates how to retrieve fax details using the
getFaxmethod.
4. Error handling:
- Wrap the API call in a
try-catchblock to handle any exceptions and print error messages.
Updated 7 months ago
