Skip to content

C# SDK Quickstart

This tutorial builds two simple C# console applications from scratch:

  1. Receive: connects and authenticates with the Invoicetronic API and download any new incoming invoices.
  2. Send: connects and authenticates with the Invoicetronic API and sends an invoice to the SDI.

Before continuing, make sure all the prerequisites below are met.

Prerequisites

We assume that these prerequisites are met:

We use the dotnet tool and VS Code as they are available on most systems, but you can follow along with your favourite IDE (Visual Studio, Rider, etc.) if you prefer.

Tip

For an optimal C# experience in VS Code, you want to ensure that the C# Dev Kit extension is installed and enabled. For more information, see Getting Started with C# in VS Code.

Receive

Create the app

The first step is to create the application:

dotnet new console -n receive
The command created a new C# project named "quickstart" in a directory with the same name. Step into that directory:

cd receive

Install the SDK

Once in the quickstart directory, install the C# SDK:

 dotnet add package Invoicetronic.Sdk

Once that's done, open VS Code in the current directory:

code .

Click on the Program.cs file to see its contents in VS Code right pane.

Configure the SDK

Replace Program.cs default content with the following:

Configure the SDK
using Invoicetronic.Sdk.Api;
using Invoicetronic.Sdk.Client;
using static Invoicetronic.Sdk.Model.Receive;

// Configure the SDK.
var config = new Configuration
{
    BasePath = "https://api.invoicetronic.com/v1",
    Username = "YOUR TEST API KEY (starts with ik_test_)"
};

As you can see, we initialize a Configuration instance by setting the API's base path and your test API Key (not the live one). Notice how we use the Username property to set the API Key.

API Key comes in pairs

When you create your account, you obtain a pair of API Keys. One is the test key for the API Sandbox, and the other is the live API's. You can tell the difference because the former starts with ik_test_, while the latter begins with ik_live_. In this tutorial, always use the test key.

Download invoices

We are ready to make a request. We want to download new vendor invoices that may be avaiable from the SDI. Add these lines:

Download unread invoices
// Download unread invoices.
var receiveApi = new ReceiveApi(config);

try
{
    var inboundInvoices = await receiveApi.ReceiveGetAsync(unread:true);
    Console.WriteLine($"Received {inboundInvoices.Count} invoices");

    foreach (var invoice in inboundInvoices)
    {
        switch (invoice.Encoding)
        {
            case EncodingEnum.Xml:
                File.WriteAllText(invoice.FileName, invoice.Payload);
                break;
            case EncodingEnum.Base64:
                File.WriteAllBytes(invoice.FileName, Convert.FromBase64String(invoice.Payload));
                break;
        }

        Console.WriteLine($"Downloaded {invoice.FileName} from a vendor with VAT ID {invoice.Prestatore}");
    }
}
catch (ApiException e)
{
    Console.WriteLine($"{e.Message} - {e.ErrorCode}");
}

Switch to the terminal(1) then type:

  1. If the terminal pane is not already open, click the Terminal menu, then New Terminal.
dotnet run

You should obtain an output similar to this one:

Received 3 invoices
Downloaded file1.xml from a vendor with VAT ID IT06157670966
Downloaded file2.xml.p7m from a vendor with VAT ID IT01280270057
Downloaded file3.xml.p7m from a vendor with VAT ID IT01280270057

The files are in the current directory, ready for you to inspect them.

Not receiving invoices in the live environment?

Ensure you registered with the Italian Revenue Service, which is a requirement for the live environment.

What we learned

In this example, we learned several things.

  1. We must configure the SDK by setting both the BasePath and Username properties, the latter initialized with the API key.

  2. We must instantiate a class representing the endpoint we want to work with. In this case, we leverage ReceiveApi to download incoming invoices.

  3. Endpoint classes like ReceiveApi offer methods for interacting with their target entity. We call InvoiceV1ReceiveGetAsync to retrieve invoices. Because we only want new, unread invoices, we pass unread: true (if you run the example a second time, you'll likely not receive any invoice unless some has arrived).

  4. The Receive class exposes valuable properties such as Encoding, FileName, and Payload. The last one contains the invoice content, as plain text or Base64-encoded, as described by Encoding.

Source Code on GitHub

The source code for this Quickstart is also available on GitHub.

Send

Create the app

The first step is to create the application:

dotnet new console -n send
The command created a new C# project named "quickstart" in a directory with the same name. Step into that directory:

cd send

Install the SDK

Once in the quickstart directory, install the C# SDK:

 dotnet add package Invoicetronic.Sdk

Once that's done, open VS Code in the current directory:

code .

Click on the Program.cs file to see its contents in VS Code right pane.

Configure the SDK

Replace Program.cs default content with the following:

Configure the SDK
using Invoicetronic.Sdk.Api;
using Invoicetronic.Sdk.Client;
using Invoicetronic.Sdk.Model;

// Configure the SDK.
var config = new Configuration
{
    BasePath = "https://api.invoicetronic.com/v1",
    Username = "YOUR TEST API KEY (starts with ik_test_)"
};

As you can see, we initialize a Configuration instance by setting the API's base path and your test API Key (not the live one). Notice how we use the Username property to set the API Key.

API Key comes in pairs

When you create your account, you obtain a pair of API Keys. One is the test key for the API Sandbox, and the other is the live API's. You can tell the difference because the former starts with ik_test_, while the latter begins with ik_live_. In this tutorial, always use the test key.

Send an invoice

We are ready to make a request. We want to send an invoice to the SDI. Add the following code:

Send and invoice
// Send an invoice
var filePath = "/some/file/path/filename.xml";

var metaData = new Dictionary<string, string>
{
    { "internal_id", "123" },
    { "created_with", "myapp" },
    { "some_other_custom_data", "value" },
};

var sendApi = new SendApi(config);

try
{
    var sentInvoice = await sendApi.SendPostAsync(new Send()
    {
        FileName = Path.GetFileName(filePath),
        Payload = File.ReadAllText(filePath),
        MetaData = metaData
    });

    Console.WriteLine($"The invoice was sent successfully, it now has the unique Id of {sentInvoice.Id}.");
}
catch (ApiException e)
{
    Console.WriteLine($"{e.Message} - {e.ErrorCode}");
}

Switch to the terminal(1) then type:

  1. If the terminal pane is not already open, click the Terminal menu, then New Terminal.
dotnet run

You should obtain an output similar to this one:

The invoice filename.xml was sent successfully, it now has the unique Id of 123.

What we learned

In this example, we learned several things.

  1. We must configure the SDK by setting both the BasePath and Username properties, the latter initialized with the API key.

  2. We must instantiate a class representing the endpoint we want to work with. In this case, we leverage SendApi to send invoices. Endpoint classes like SendApi offer methods for interacting with their target entity. We call InvoiceV1SendPosttAsync to send an invoice.

  3. The Send class exposes valuable properties such as FileName, MetaData, and Payload. The last one contains the invoice content, while MetaData is optional and binds custom data to the document.

Source Code on GitHub

The source code for this Quickstart is also available on GitHub.