This document provides examples of using various Asaas SDK services.
using Asaas.Sdk;
using Asaas.Sdk.Config;
using Asaas.Sdk.Http;
var config = new AsaasSdkConfig
{
ApiKeyAuthConfig = new ApiKeyAuthConfig
{
ApiKey = "YOUR_API_KEY"
}
};
var sdk = new AsaasSdk(config);
sdk.SetEnvironment(Environment.Sandbox); // Use sandbox for testingvar payment = await sdk.Payment.CreatePaymentAsync(new
{
customer = "cus_000000000001",
billingType = "BOLETO",
value = 100.00,
dueDate = DateTime.Today.AddDays(7).ToString("yyyy-MM-dd"),
description = "Payment for services"
});var payments = await sdk.Payment.ListPaymentsAsync(
offset: 0,
limit: 10,
status: "PENDING"
);var updated = await sdk.Payment.UpdatePaymentAsync(
"pay_000000000001",
new { description = "Updated description" }
);await sdk.Payment.DeletePaymentAsync("pay_000000000001");var customer = await sdk.Customer.CreateCustomerAsync(new
{
name = "John Doe",
cpfCnpj = "12345678900",
email = "john@example.com",
phone = "11999999999",
mobilePhone = "11988888888",
address = "123 Main St",
addressNumber = "100",
complement = "Apt 5",
province = "Center",
postalCode = "01000-000"
});var customers = await sdk.Customer.ListCustomersAsync(
offset: 0,
limit: 20
);var subscription = await sdk.Subscription.CreateSubscriptionAsync(new
{
customer = "cus_000000000001",
billingType = "CREDIT_CARD",
value = 49.90,
nextDueDate = DateTime.Today.AddMonths(1).ToString("yyyy-MM-dd"),
cycle = "MONTHLY",
description = "Monthly subscription"
});var subscriptions = await sdk.Subscription.ListSubscriptionsAsync(
offset: 0,
limit: 10
);await sdk.Subscription.DeleteSubscriptionAsync("sub_000000000001");var pixKey = await sdk.Pix.CreatePixKeyAsync(new
{
type = "EVP",
addressKey = null // Auto-generate EVP key
});var qrCode = await sdk.Pix.CreateQrCodeAsync(new
{
addressKey = "your-pix-key",
description = "Payment via PIX",
value = 50.00
});var transaction = await sdk.PixTransaction.CreatePixTransactionAsync(new
{
value = 100.00,
pixAddressKey = "recipient-pix-key",
description = "Transfer via PIX"
});var decoded = await sdk.PixTransaction.DecodeQrCodeAsync(new
{
payload = "00020126330014BR.GOV.BCB.PIX..."
});var transfer = await sdk.Transfer.CreateTransferAsync(new
{
value = 1000.00,
bankAccount = new
{
bank = new { code = "341" },
accountName = "John Doe",
ownerName = "John Doe",
ownerBirthDate = "1990-01-01",
cpfCnpj = "12345678900",
agency = "1234",
account = "12345-6",
accountDigit = "6"
}
});var transfers = await sdk.Transfer.ListTransfersAsync(
offset: 0,
limit: 10
);var webhook = await sdk.Webhook.CreateWebhookAsync(new
{
url = "https://your-domain.com/webhook",
email = "admin@your-domain.com",
enabled = true,
interrupted = false,
events = new[]
{
"PAYMENT_CREATED",
"PAYMENT_UPDATED",
"PAYMENT_CONFIRMED"
}
});var webhooks = await sdk.Webhook.ListWebhooksAsync(
offset: 0,
limit: 10
);var updated = await sdk.Webhook.UpdateWebhookAsync(
"webhook_id",
new { enabled = false }
);var subaccount = await sdk.Subaccount.CreateSubaccountAsync(new
{
name = "Partner Store",
email = "partner@example.com",
cpfCnpj = "12345678900",
birthDate = "1990-01-01",
phone = "11999999999",
mobilePhone = "11988888888",
address = "123 Store St",
addressNumber = "200",
postalCode = "01000-000"
});var subaccounts = await sdk.Subaccount.ListSubaccountsAsync(
offset: 0,
limit: 10
);var invoice = await sdk.Invoice.CreateInvoiceAsync(new
{
customer = "cus_000000000001",
serviceDescription = "Consulting services",
observations = "Monthly consulting - January 2024",
value = 1500.00,
effectiveDate = DateTime.Today.ToString("yyyy-MM-dd")
});var authorized = await sdk.Invoice.AuthorizeInvoiceAsync("invoice_id");var balance = await sdk.Finance.GetBalanceAsync();
Console.WriteLine($"Current Balance: {balance.balance}");var stats = await sdk.Finance.GetStatisticsAsync();
Console.WriteLine($"Total received: {stats.value}");var transactions = await sdk.FinancialTransaction.ListFinancialTransactionsAsync(
startDate: DateTime.Today.AddDays(-30),
endDate: DateTime.Today,
offset: 0,
limit: 50
);var paymentLink = await sdk.PaymentLink.CreatePaymentLinkAsync(new
{
name = "Product Purchase",
description = "Purchase of Product XYZ",
billingType = "UNDEFINED",
chargeType = "DETACHED",
value = 99.90
});var simulation = await sdk.Anticipation.SimulateAnticipationAsync(new
{
payment = "pay_000000000001"
});var anticipation = await sdk.Anticipation.CreateAnticipationAsync(new
{
payment = "pay_000000000001"
});await sdk.SandboxActions.SimulatePaymentReceivedAsync("pay_000000000001");await sdk.SandboxActions.SimulatePaymentConfirmationAsync("pay_000000000001");await sdk.SandboxActions.SimulateChargebackAsync("pay_000000000001");var pixSimulation = await sdk.SandboxActions.SimulatePixTransactionReceivedAsync(new
{
payment = "pay_000000000001",
value = 100.00
});Always wrap SDK calls in try-catch blocks:
using Asaas.Sdk.Exceptions;
try
{
var payment = await sdk.Payment.CreatePaymentAsync(paymentData);
}
catch (ApiException ex)
{
Console.WriteLine($"API Error: {ex.Message}");
Console.WriteLine($"Status Code: {ex.StatusCode}");
if (ex.Errors != null)
{
foreach (var error in ex.Errors)
{
Console.WriteLine($"- {error.Code}: {error.Description}");
}
}
}
catch (ValidationException ex)
{
Console.WriteLine($"Validation Error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected Error: {ex.Message}");
}