Skip to content

formancehq/formance-sdk-csharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

formance

Developer-friendly & type-safe Csharp SDK specifically catered to leverage formance API.

Summary

Formance Stack API: Open, modular foundation for unique payments flows

Introduction

This API is documented in OpenAPI format.

Authentication

Formance Stack offers one forms of authentication:

  • OAuth2 OAuth2 - an open protocol to allow secure authorization in a simple and standard method from web, mobile and desktop applications.

Table of Contents

SDK Installation

NuGet

To add the NuGet package to a .NET project:

dotnet add package FormanceSDK

Locally

To add a reference to a local instance of the SDK in a .NET project:

dotnet add reference src/FormanceSDK/FormanceSDK.csproj

SDK Example Usage

Example

using FormanceSDK;
using FormanceSDK.Models.Components;

var sdk = new Formance(security: new Security() {
    ClientID = "<YOUR_CLIENT_ID_HERE>",
    ClientSecret = "<YOUR_CLIENT_SECRET_HERE>",
});

var res = await sdk.GetVersionsAsync();

// handle response

Available Resources and Operations

Available methods

Error Handling

FormanceError is the base exception class for all HTTP error responses. It has the following properties:

Property Type Description
Message string Error message
Request HttpRequestMessage HTTP request object
Response HttpResponseMessage HTTP response object

Some exceptions in this SDK include an additional Payload field, which will contain deserialized custom error data when present. Possible exceptions are listed in the Error Classes section.

Example

using FormanceSDK;
using FormanceSDK.Models.Components;
using FormanceSDK.Models.Errors;

var sdk = new Formance(security: new Security() {
    ClientID = "<YOUR_CLIENT_ID_HERE>",
    ClientSecret = "<YOUR_CLIENT_SECRET_HERE>",
});

try
{
    var res = await sdk.Ledger.V1.GetInfoAsync();

    // handle response
}
catch (FormanceError ex)  // all SDK exceptions inherit from FormanceError
{
    // ex.ToString() provides a detailed error message
    System.Console.WriteLine(ex);

    // Base exception fields
    HttpRequestMessage request = ex.Request;
    HttpResponseMessage response = ex.Response;
    var statusCode = (int)response.StatusCode;
    var responseBody = ex.Body;

    if (ex is Models.Errors.ErrorResponse) // different exceptions may be thrown depending on the method
    {
        // Check error data fields
        Models.Errors.ErrorResponsePayload payload = ex.Payload;
        ErrorsEnum ErrorCode = payload.ErrorCode;
        string ErrorMessage = payload.ErrorMessage;
        // ...
    }

    // An underlying cause may be provided
    if (ex.InnerException != null)
    {
        Exception cause = ex.InnerException;
    }
}
catch (System.Net.Http.HttpRequestException ex)
{
    // Check ex.InnerException for Network connectivity errors
}

Error Classes

Primary exception:

Less common exceptions (11)

* Refer to the relevant documentation to determine whether an exception applies to a specific operation.

Server Selection

Select Server by Index

You can override the default server globally by passing a server index to the serverIndex: int optional parameter when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:

# Server Variables Description
0 http://localhost local server
1 https://{organization}.{environment}.formance.cloud organization
environment
A per-organization and per-environment API

If the selected server has variables, you may override its default values through the additional parameters made available in the SDK constructor:

Variable Parameter Supported Values Default Description
organization organization: string string "orgID-stackID" The organization name. Defaults to a generic organization.
environment environment: FormanceSDK.Models.ServerEnvironment - "eu.sandbox"
- "sandbox"
- "eu-west-1"
- "us-east-1"
"eu.sandbox" The environment name. Defaults to the production environment.

Example

using FormanceSDK;
using FormanceSDK.Models.Components;

var sdk = new Formance(
    serverIndex: 1,
    organization: "<value>",
    environment: "us-east-1",
    security: new Security() {
        ClientID = "<YOUR_CLIENT_ID_HERE>",
        ClientSecret = "<YOUR_CLIENT_SECRET_HERE>",
    }
);

var res = await sdk.GetVersionsAsync();

// handle response

Override Server URL Per-Client

The default server can also be overridden globally by passing a URL to the serverUrl: string optional parameter when initializing the SDK client instance. For example:

using FormanceSDK;
using FormanceSDK.Models.Components;

var sdk = new Formance(
    serverUrl: "https://orgID-stackID.eu.sandbox.formance.cloud",
    security: new Security() {
        ClientID = "<YOUR_CLIENT_ID_HERE>",
        ClientSecret = "<YOUR_CLIENT_SECRET_HERE>",
    }
);

var res = await sdk.GetVersionsAsync();

// handle response

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

Name Type Scheme
ClientID
ClientSecret
oauth2 OAuth2 Client Credentials Flow

You can set the security parameters through the security optional parameter when initializing the SDK client instance. For example:

using FormanceSDK;
using FormanceSDK.Models.Components;

var sdk = new Formance(security: new Security() {
    ClientID = "<YOUR_CLIENT_ID_HERE>",
    ClientSecret = "<YOUR_CLIENT_SECRET_HERE>",
});

var res = await sdk.GetVersionsAsync();

// handle response

Development

Maturity

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.

SDK Created by Speakeasy

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 6

Languages