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

  1. Environment variables:

    • Ensure the following environment variables are set:
      • ENVIROMENT: The environment to use (prod or nonprod).
      • API_KEY: Your API key for authentication.
  2. Node.js version:

    • Ensure you are using Node.js version 14 or higher.

Configuration

  1. Set up environment variables:

    • Use a .env file to configure the SDK:
      ENVIRONMENT=nonprod
      API_KEY=your-api-key
  2. Load configuration:

    • Use the dotenv package to load environment variables:
      import * as dotenv from 'dotenv';
      dotenv.config();
  3. 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');


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 (ENVIROMENT and API_KEY).\

2. Initialize the SDK:

  • The v1 function initializes the SDK with the provided environment and API key.

3. Fax API example:

  • Demonstrates how to retrieve fax details using the getFax method.

4. Error handling:

  • Wrap the API call in a try-catch block to handle any exceptions and print error messages.