Blacus.TropicAPIWrapper
10.0.1
dotnet add package Blacus.TropicAPIWrapper --version 10.0.1
NuGet\Install-Package Blacus.TropicAPIWrapper -Version 10.0.1
<PackageReference Include="Blacus.TropicAPIWrapper" Version="10.0.1" />
<PackageVersion Include="Blacus.TropicAPIWrapper" Version="10.0.1" />
<PackageReference Include="Blacus.TropicAPIWrapper" />
paket add Blacus.TropicAPIWrapper --version 10.0.1
#r "nuget: Blacus.TropicAPIWrapper, 10.0.1"
#:package Blacus.TropicAPIWrapper@10.0.1
#addin nuget:?package=Blacus.TropicAPIWrapper&version=10.0.1
#tool nuget:?package=Blacus.TropicAPIWrapper&version=10.0.1
Blacus.TropicAPIWrapper
A modern .NET 10 client library for interacting with the Tropic API.
Features
- Typed Models & Resources: Clean, strongly-typed wrappers for Contracts, Purchase Requests, Comments, and Suppliers using standard auto-properties.
- Direct Resource Access: Idiomatic resource calls via
client.Contracts,client.PurchaseRequests,client.Comments,client.Suppliers, andclient.Webhooks. - Interface Abstraction & DI Support: Full
ITropicClientand resource interfaces (IContractsResource,IPurchaseRequestsResource, etc.) with turnkeyservices.AddTropicClient()dependency injection extensions for ASP.NET Core. - Safe HttpClient Integration: Per-request headers and relative URI resolution ensure shared
HttpClientinstances (e.g. fromIHttpClientFactory) are never mutated. - Automatic Rate Limit Handling: Built-in exponential backoff retry mechanism for HTTP 429 (
TooManyRequests), handling both delta and date-basedRetry-Afterheaders. - Webhook Verification: Timing-safe HMAC-SHA256 verification of
Tropic-SignatureandTropic-Timestampheaders with optional anti-replay timestamp tolerance. - Zero Third-Party Dependencies: Pure .NET 10 BCL implementation with no external runtime dependencies.
Target Framework
.NET 10.0(net10.0)
Installation
Add the project reference or package to your .NET project:
dotnet add reference path/to/Blacus.TropicAPIWrapper.csproj
Quickstart
Dependency Injection (ASP.NET Core / Generic Host)
Register ITropicClient directly into your service collection in Program.cs:
using Microsoft.Extensions.DependencyInjection;
// Option 1: Bind from configuration section ("Tropic" in appsettings.json)
builder.Services.AddTropicClient();
// Option 2: Provide API key directly
builder.Services.AddTropicClient("your_tropic_api_key");
// Option 3: Full options configuration
builder.Services.AddTropicClient(options =>
{
options.ApiKey = builder.Configuration["Tropic:ApiKey"]!;
options.BaseUrl = "https://app.tropicapp.io";
});
When registered with AddTropicClient, you can inject ITropicClient, the concrete TropicClient, or any individual resource (e.g., IContractsResource, ISuppliersResource) directly into your services:
public class ProcurementService(IContractsResource contractsResource)
{
public async Task<List<ContractModel>?> GetActiveContractsAsync()
{
return await contractsResource.GetAllAsync();
}
}
Standalone Client Initialization
using Blacus.TropicAPIWrapper;
// Basic initialization
using var client = new TropicClient("your_tropic_api_key");
// Or with an existing shared HttpClient and custom base URL
using var customClient = new TropicClient(
apiKey: "your_tropic_api_key",
httpClient: sharedHttpClient,
baseUrl: "https://app.tropicapp.io"
);
API Usage Examples
All asynchronous operations follow the standard .NET Task-based Asynchronous Pattern (TAP) using the *Async naming convention (GetAllAsync, GetPageAsync, GetAsync, CreateAsync, UpdateAsync, SearchAsync).
1. Contracts
using Blacus.TropicAPIWrapper.Models;
// List all contracts (auto-paginates all pages)
List<ContractModel>? contracts = await client.Contracts.GetAllAsync();
// Fetch a single page of contracts with pagination metadata
TropicPagedResponse<ContractModel>? page = await client.Contracts.GetPageAsync(page: 1, perPage: 50);
// Fetch a single contract by ID
ContractModel? contract = await client.Contracts.GetAsync("contract_12345");
// Create a new contract
var newContract = await client.Contracts.CreateAsync(new ContractRequest
{
Name = "Cloud Infrastructure Annual Agreement",
SupplierId = "supp_12345",
Status = "active",
TotalValue = 50000m,
Currency = "USD",
StartDate = DateTimeOffset.UtcNow,
EndDate = DateTimeOffset.UtcNow.AddYears(1),
AutoRenew = true
});
// Update an existing contract
var updatedContract = await client.Contracts.UpdateAsync("contract_12345", new ContractRequest
{
Name = "Cloud Infrastructure Annual Agreement - Renewal",
TotalValue = 55000m
});
2. Purchase Requests
using Blacus.TropicAPIWrapper.Models;
// List all purchase requests (auto-paginates across all pages)
List<PurchaseRequestModel>? requests = await client.PurchaseRequests.GetAllAsync();
// Fetch a single page of purchase requests with pagination metadata
TropicPagedResponse<PurchaseRequestModel>? page = await client.PurchaseRequests.GetPageAsync(page: 1, perPage: 50);
// Fetch a single purchase request by ID
PurchaseRequestModel? request = await client.PurchaseRequests.GetAsync("pr_12345");
// Update a purchase request
var updatedRequest = await client.PurchaseRequests.UpdateAsync("pr_12345", new PurchaseRequestUpdateRequest
{
Status = "approved",
Amount = 12000m,
Currency = "USD",
Description = "Approved marketing software licenses"
});
3. Comments
using Blacus.TropicAPIWrapper.Models;
// Get comments for a purchase request
List<CommentModel>? comments = await client.Comments.GetAllAsync("pr_12345");
// Add a comment to a purchase request
var comment = await client.Comments.CreateAsync("pr_12345", new CommentCreateRequest
{
Body = "Reviewed and approved by procurement team."
});
4. Suppliers
using Blacus.TropicAPIWrapper.Models;
// Search suppliers by keyword
List<SupplierModel>? searchResults = await client.Suppliers.SearchAsync("Acme");
// Fetch supplier by ID
SupplierModel? supplier = await client.Suppliers.GetAsync("supp_12345");
// Create a supplier
var newSupplier = await client.Suppliers.CreateAsync(new SupplierRequest
{
Name = "Acme Cloud Services",
Website = "https://acme.example.com",
ContactName = "Jane Doe",
ContactEmail = "jane.doe@acme.example.com",
ContactPhone = "+1-555-0100"
});
// Update a supplier
var updatedSupplier = await client.Suppliers.UpdateAsync("supp_12345", new SupplierRequest
{
Name = "Acme Cloud Services Inc."
});
5. Webhook Verification & Payloads
Tropic sends incoming webhooks with Tropic-Timestamp and Tropic-Signature HTTP headers. You can verify signatures and optionally enforce a timestamp tolerance to guard against replay attacks:
using System.Text.Json;
using Blacus.TropicAPIWrapper.Models;
// Verify webhook signature with a 5-minute replay tolerance window
bool isValid = client.Webhooks.Verify(
webhookSecret: "your_webhook_signing_secret",
tropicTimestampHeader: request.Headers["Tropic-Timestamp"],
rawRequestBody: rawPayloadString,
tropicSignatureHeader: request.Headers["Tropic-Signature"],
tolerance: TimeSpan.FromMinutes(5)
);
if (!isValid)
{
return Results.Unauthorized();
}
// Deserialize the webhook payload
var payload = JsonSerializer.Deserialize<WebhookEventPayload<PurchaseRequestModel>>(rawPayloadString);
Console.WriteLine($"Received event {payload?.EventType} for ID {payload?.Data?.Id}");
Error Handling
When the Tropic API returns an unsuccessful status code, a TropicApiException is thrown containing the HTTP status code and response body:
using Blacus.TropicAPIWrapper;
try
{
var contract = await client.Contracts.UpdateAsync("invalid_id", new ContractRequest());
}
catch (TropicApiException ex)
{
Console.WriteLine($"Status: {ex.StatusCode} ({(int)ex.StatusCode})");
Console.WriteLine($"Message: {ex.Message}");
Console.WriteLine($"Response Body: {ex.ResponseBody}");
}
Building and Packaging
To compile the library locally using the .NET SDK:
dotnet build src/Blacus.TropicAPIWrapper/Blacus.TropicAPIWrapper.csproj
To pack the NuGet package and symbols:
dotnet pack src/Blacus.TropicAPIWrapper/Blacus.TropicAPIWrapper.csproj -c Release
Publishing to NuGet
Automated publishing is configured via GitHub Actions in .github/workflows/publish.yml.
Prerequisites
Add your NuGet API key as a repository secret:
- Navigate to Settings > Secrets and variables > Actions in your GitHub repository.
- Add a new repository secret named
NUGET_API_KEYwith your API key from nuget.org.
Releasing via Git Tag
Pushing a version tag automatically builds, packs, publishes the package to NuGet.org, and creates a GitHub Release:
git tag v1.0.0
git push origin v1.0.0
Manual Trigger
You can also trigger a release manually from GitHub Actions:
- Go to Actions > Publish to NuGet.
- Click Run workflow, optionally specifying a version override and whether to run in
dry_runmode.
License
This project is licensed under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.Http (>= 10.0.14)
- Microsoft.Extensions.Options.DataAnnotations (>= 10.0.14)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.