-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate.cs
More file actions
94 lines (88 loc) · 3.47 KB
/
Copy pathCreate.cs
File metadata and controls
94 lines (88 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Create a UN/CEFACT Cross Industry Invoice (CII D16B, EN 16931 compliant) using
// the InvoiceXML REST API. Sends a JSON invoice model and receives the XML.
//
// InvoiceXML: https://www.invoicexml.com/
// Get your API key at: https://www.invoicexml.com/account/authentication
// Full API reference: https://www.invoicexml.com/docs/api/create/cii
//
// Required NuGet package:
// dotnet add package Flurl.Http
using Flurl.Http;
public static class CreateCii
{
private const string Endpoint = "https://api.invoicexml.com/v1/create/cii";
public static async Task<string> RunAsync(string outputPath = "invoice-cii.xml")
{
// Get an InvoiceXML API key at:
// https://www.invoicexml.com/account/authentication
// Raw key only, without the "Bearer " prefix (WithOAuthBearerToken adds it).
var apiKey = Environment.GetEnvironmentVariable("INVOICEXML_API_KEY")
?? throw new InvalidOperationException(
"Set INVOICEXML_API_KEY environment variable. " +
"Get an InvoiceXML API key at " +
"https://www.invoicexml.com/account/authentication");
// Minimal CII invoice payload; totals and the VAT breakdown are calculated
// from the line items. Full field reference:
// https://www.invoicexml.com/docs/api/create/cii
var payload = new
{
invoice = new
{
invoiceNumber = "CII-2026-001",
issueDate = "2026-05-18",
currency = "EUR",
seller = new
{
name = "Acme GmbH",
vatIdentifier = "DE123456789",
legalRegistration = new { identifier = "HRB 12345" },
postalAddress = new
{
line1 = "Hauptstraße 12",
city = "Berlin",
postCode = "10115",
country = "DE"
}
},
buyer = new
{
name = "Globex SAS",
postalAddress = new
{
line1 = "15 rue de Rivoli",
city = "Paris",
postCode = "75001",
country = "FR"
}
},
paymentDetails = new { paymentAccountIdentifier = "DE89370400440532013000" },
lines = new[]
{
new
{
quantity = 10,
priceDetails = new { netPrice = 150.00m },
vatInformation = new { rate = 19.00m },
item = new { name = "Senior consulting" }
}
}
}
};
try
{
var xml = await Endpoint
.WithOAuthBearerToken(apiKey)
.PostJsonAsync(payload)
.ReceiveString();
await File.WriteAllTextAsync(outputPath, xml);
Console.WriteLine($"CII invoice saved: {outputPath} ({xml.Length:N0} chars)");
return xml;
}
catch (FlurlHttpException ex)
{
var body = await ex.GetResponseStringAsync();
Console.Error.WriteLine($"InvoiceXML API error {(int?)ex.StatusCode}: {body}");
throw;
}
}
}