C#
The C# SDK enables seamless integration with Optum Connections APIs for .NET applications. It provides strongly-typed models, dependency injection support, and asynchronous methods to simplify API interactions. Designed for ease of use, the SDK helps developers quickly configure and access financial services with minimal setup.
Installation
To use the Optum.Financial.Sdk, you need to install the NuGet package. You can do this by running the following command in your terminal:
dotnet add package Optum.Financial.SdkEnsure that your project includes a NuGet.Config file with the appropriate source configuration if needed.
Prerequisites
- A .NET runtime that supports NET Standard 2.0
- The .NET CLI
- Make sure to import the following NuGet packages to work with the SDK and read configuration values:
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Configuration.Binder
- Microsoft.Extensions.Configuration.Json
- Microsoft.Extensions.DependencyInjection
Configuration
1. Update configuration values
- Add or update the Environment and ApiKey values in your appsettings.json file under the SdkConfiguration section. Example:
{
"SdkConfiguration": {
"Environment": "your-environment",
"ApiKey": "your-api-key"
}
}2. Read configuration values:
- Use the following code to read the configuration values from appsettings.json:
var sdkConfiguration = configuration.GetSection("SdkConfiguration").Get<SdkConfiguration>();3. Register API services:
- Use the ConfigureV1Apis extension method to register the API services:
services.ConfigureV1Apis(sdkConfiguration.Environment, sdkConfiguration.ApiKey);Usage
Here’s an example of how to use the SDK to retrieve fax details:
using System;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Optum.Financial.Sdk;
using Optum.Financial.Sdk.V1;
using Optum.Financial.Sdk.V1.Models.FaxSection;
class Program
{
static async Task Main(string[] args)
{
// Configuration
var builder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
var configuration = builder.Build();
var sdkConfiguration = configuration.GetSection("SdkConfiguration").Get<SdkConfiguration>();
if (sdkConfiguration is null || string.IsNullOrWhiteSpace(sdkConfiguration.Environment) || string.IsNullOrWhiteSpace(sdkConfiguration.ApiKey))
{
Console.Error.WriteLine("*** Error: SDK Configuration is missing or incomplete. Please check your appsettings.json or environment variables.");
return;
}
// Dependency Injection
var serviceProvider = new ServiceCollection()
.ConfigureV1Apis(sdkConfiguration.Environment, sdkConfiguration.ApiKey)
.BuildServiceProvider();
// Example: Fax API
var paymentsApi = serviceProvider.GetRequiredService<PaymentsApi>();
var faxApi = paymentsApi.Fax;
try
{
Console.WriteLine("Testing FaxApi.GetFaxAsync method");
// Replace with your fax ID
var faxId = new Guid("856bdd48-e709-bee7-619d-caa40757c7f2");
FaxDetails result = await faxApi.GetFaxAsync(faxId);
Console.WriteLine(JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true }));
}
catch (Exception e)
{
Console.Error.WriteLine($"Error: {e.Message}");
}
}
}Explanation of the code
1. Configuration:
- Reads the SDK configuration (e.g.,
EnvironmentandApiKey) fromappsettings.jsonor environment variables.
2. Dependency injection:
- Registers the SDK services using
ConfigureV1Apis.
3.Fax API example:
- Demonstrates how to retrieve fax details using
paymentsApi.Fax.GetFaxAsync.
4. Error handling:
- Wraps the API call in a
try-catchblock to handle exceptions and log errors.
Updated 7 months ago
