Pular para o conteúdo principal

Full example in .NET

The console application below goes through a full integration with the .NET client library. It chooses the certificate type, creates the order, pre-fills the holder data, sends a document and lists the status changes.

Before you start​

  • The endpoint and an API key of an application with the Sales app role. See Get started.
  • The .NET 8 SDK or later.

The example creates a real order on the instance. If the instance has the RFB prior query configured, the order creation checks the holder's data. Use real data, with the name as the Brazilian Revenue Service has it. To cancel the order afterwards, see Cancel an order.

Create the project​

  1. Create a console application:

    dotnet new console -n AmpliaRegExample
  2. Go to the project folder:

    cd AmpliaRegExample
  3. Add the client library and the hosting package:

    dotnet add package Lacuna.AmpliaRegNg.Client
    dotnet add package Microsoft.Extensions.Hosting
  4. Replace the content of Program.cs with the code in the next section.

  5. In the code, replace the endpoint, the API key, the holder data and the file path with your values.

  6. Run the project:

    dotnet run

Code​

using Lacuna.AmpliaRegNg.Api;
using Lacuna.AmpliaRegNg.Api.CertificateTypes;
using Lacuna.AmpliaRegNg.Api.Contact;
using Lacuna.AmpliaRegNg.Api.Holders;
using Lacuna.AmpliaRegNg.Api.Orders;
using Lacuna.AmpliaRegNg.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddAmpliaReg(options => {
options.Endpoint = "https://ampliareg.yourcompany.com/";
options.ApiKey = "your-app|YOUR_KEY";
});

using var host = builder.Build();
var ampliaReg = host.Services.GetRequiredService<IAmpliaRegService>();

// Holder data
var cpf = "07917307000";
var name = "Holder Name"; // Name as the Brazilian Revenue Service has it
var birthDate = new DateTime(1995, 10, 15);

// 1. List the certificate types and choose an e-CPF type
var certTypes = await ampliaReg.ListCertificateTypesAsync(new CertificateTypeListParameters {
IsDisabled = false,
});
var certType = certTypes.First(c => c.BrazilParameters?.BrazilType == PkiBrazilCertificateTypes.PessoaFisica);
Console.WriteLine($"Certificate type: {certType.Name} ({certType.Code})");

// 2. Create the order
var order = await ampliaReg.CreateOrderAsync(new OrderCreateRequest<BrazilOrderCreateParameters> {
CertificateType = CertificateTypeReference.FromCode(certType.Code),
IssueType = IssueTypes.Presential,
Parameters = new BrazilOrderCreateParameters {
Cpf = cpf,
Name = name,
BirthDate = birthDate.ToString("yyyy-MM-dd"),
},
Validity = certType.ValidityOptions.First(),
});
Console.WriteLine($"Order created: {order.Number} ({order.Id})");

// 3. Pre-fill the holder's address and contact
await ampliaReg.PreFillHolderAsync(order.Number, new UpdateHolderInformationRequest<BrazilUpdateHolderParameters> {
Parameters = new BrazilUpdateHolderParameters {
Address = new BrazilHolderAddressRequest {
ZipCode = "70000-000",
Street = "Rua Exemplo",
Number = "100",
District = "Centro",
City = "Brasília",
StateCode = "DF",
Country = "Brazil",
},
},
Contact = new ContactData {
Phone = "+5511999990000",
EmailAddress = "holder@example.com",
},
});

// 4. Send the holder documents. The library uploads the files
await ampliaReg.AddHolderFilesAsync(
order.Number,
new List<HolderFileReference2> {
new HolderFileReference2 {
Type = HolderFileTypes.Identification.ToString(),
Subtype = HolderFileSubtypes.Cnh,
DocumentPart = DocumentParts.FrontComplete,
File = FileReference.FromFile(@"C:\AppData\Documents\cnh.pdf"),
},
},
HolderTypeReference.FromCode("PF")
);

// 5. List the status changes of the last day, one page at a time
var startDate = DateTimeOffset.Now.AddDays(-1);
var offset = 0;
while (true) {
var page = await ampliaReg.ListOrderStatusTransitionsAsync(new OrderStatusTransitionSearchParams {
StartDate = startDate,
Offset = offset,
Limit = 100,
});
if (!page.Items.Any()) {
break;
}
foreach (var change in page.Items) {
Console.WriteLine($"Order {change.OrderNumber}: {change.PreviousStatus} -> {change.Status} on {change.Date}");
}
offset += page.Items.Count();
}

What each step does​

StepCallPage
1Lists the enabled certificate types and chooses an e-CPF typeCreate orders
2Creates the order with in-person validation. The Number in the response is the order numberCreate orders
3Sends the address and the contact of the holder, so that the agent does not have to type themPre-fill the holder data
4Sends the front of the holder's driver's licenseHolder documents
5Lists the status changes of the orders of the applicationRecover missed changes

In production, receive the status changes by webhook. Use the list in step 5 only to recover the changes that your application missed.

See also​

View as Markdown