C# (.NET)
Integrate the Flimpa API into your C# applications.
Installation
This example uses the native HttpClient and System.Text.Json available in modern .NET. No external dependencies are required.
Initialization
Set up your API key and base URL.
csharp
static readonly string ApiKey = "your_api_key";
static readonly string BaseUrl = "https://api.flimpa.com/api/v1";
static readonly HttpClient client = new HttpClient();Authentication
Authenticate your requests using the X-API-Key header for task creation, and the X-Task-Access-Token header for status and download requests.
Usage Example
This example demonstrates how to create a task, poll for its status, and download the result. Operations supported are compress-pdf and image-to-webp.
csharp
using System;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
class Program
{
static readonly string ApiKey = "your_api_key";
static readonly string BaseUrl = "https://api.flimpa.com/api/v1";
static readonly HttpClient client = new HttpClient();
static async Task Main()
{
// 1. Create Task (Upload)
using var form = new MultipartFormDataContent();
form.Add(new StringContent("compress-pdf"), "operation");
using var fileStream = File.OpenRead("input.pdf");
form.Add(new StreamContent(fileStream), "file", "input.pdf");
client.DefaultRequestHeaders.Add("X-API-Key", ApiKey);
var createRes = await client.PostAsync($"{BaseUrl}/tasks", form);
if (!createRes.IsSuccessStatusCode) {
var errorBody = await createRes.Content.ReadAsStringAsync();
var errorData = JsonDocument.Parse(errorBody).RootElement;
throw new Exception($"Failed to create task: {errorData.GetProperty("error").GetString()} ({errorData.GetProperty("code").GetString()})");
}
var createBody = await createRes.Content.ReadAsStringAsync();
var taskData = JsonDocument.Parse(createBody).RootElement;
var taskId = taskData.GetProperty("taskId").GetString();
var accessToken = taskData.GetProperty("accessToken").GetString();
// 2. Poll Status
int delay = 1000;
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("X-Task-Access-Token", accessToken);
while (true)
{
var statusRes = await client.GetStringAsync($"{BaseUrl}/tasks/{taskId}");
var statusData = JsonDocument.Parse(statusRes).RootElement;
var status = statusData.GetProperty("status").GetString();
if (status == "COMPLETED") break;
if (status == "FAILED") {
throw new Exception($"Task failed: {statusData.GetProperty("error").GetString()} ({statusData.GetProperty("code").GetString()})");
}
await Task.Delay(delay);
delay = Math.Min(delay * 2, 10000);
}
// 3. Download Result
var downloadRes = await client.GetAsync($"{BaseUrl}/tasks/{taskId}/download");
downloadRes.EnsureSuccessStatusCode();
using var fs = new FileStream("output.pdf", FileMode.Create);
await downloadRes.Content.CopyToAsync(fs);
Console.WriteLine("Success!");
}
}Error Handling
Always check the response status and handle potential errors. The API returns JSON with error and code fields when a request fails.
Best Practices
- Implement exponential backoff for polling (as shown in the example).
- Files are limited to 20MB.
- Tasks have a TTL of 1 hour. Download your results promptly.
- Do not expose your API key in client-side code.
Webhooks
Webhooks for task completion are Coming Soon. For now, please use polling.