Skip to main content

Quick Start for C#

C#

Integrate PDFBolt's REST API in C# to generate PDFs from URLs, HTML, or templates. The examples below cover all three conversion modes (Direct, Sync, Async).

1. Get Your API Key

Find your API key on the API Keys page in your Dashboard. If you don't have an account, sign up – the free plan includes 100 document conversions per month.

2. Make Your First Request

Any HTTP client works – adjust the request structure to match your library.

Examples use the built-in HttpClient and System.Text.Json.

Choose your endpoint:

The Direct endpoint provides immediate PDF generation and returns the raw PDF file in the response.

Choose your source:

Convert a webpage to PDF:

using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

public class DirectUrl {
public static async Task Main(string[] args) {
using var client = new HttpClient();

var requestData = new {
url = "https://example.com",
format = "A4",
printBackground = true
};

var request = new HttpRequestMessage {
Method = HttpMethod.Post,
RequestUri = new Uri("https://api.pdfbolt.com/v1/direct"),
Content = new StringContent(
JsonSerializer.Serialize(requestData),
Encoding.UTF8,
"application/json"
)
};

request.Headers.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");

try {
using var response = await client.SendAsync(request);

if (!response.IsSuccessStatusCode) {
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"HTTP {(int)response.StatusCode}");
Console.WriteLine($"Error Message: {errorContent}");
return;
}

var pdfBytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("webpage.pdf", pdfBytes);
Console.WriteLine("PDF generated successfully");
} catch (Exception ex) {
Console.WriteLine($"Error: {ex.Message}");
}
}
}

Next Steps



Related reading