🔷 C# / .NET Integration — Quick Start

Integrate SnapAPI into your .NET application using the built-in HttpClient. No external packages required. Works with .NET 6+, ASP.NET Core, and console apps.

On This Page

Setup

No NuGet packages needed — use the built-in HttpClient and System.Text.Json:

using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;

var client = new HttpClient();
client.BaseAddress = new Uri("https://api.snapapi.pics");
client.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("SNAPAPI_KEY"));
💡 Tip: Use IHttpClientFactory in ASP.NET Core for proper connection management. See the DI section below.

Take Screenshots

Basic Screenshot

using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;

var client = new HttpClient();
client.BaseAddress = new Uri("https://api.snapapi.pics");
client.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("SNAPAPI_KEY"));

var payload = new
{
    url = "https://example.com",
    format = "png",
    width = 1280,
    height = 800
};

var content = new StringContent(
    JsonSerializer.Serialize(payload),
    Encoding.UTF8,
    "application/json"
);

var response = await client.PostAsync("/screenshot", content);
var imageBytes = await response.Content.ReadAsByteArrayAsync();

await File.WriteAllBytesAsync("screenshot.png", imageBytes);
Console.WriteLine("Screenshot saved!");

Full Page Screenshot

var payload = new
{
    url = "https://example.com",
    format = "png",
    fullPage = true,
    width = 1280
};

Screenshot with Options

var payload = new
{
    url = "https://example.com",
    format = "webp",              // png, jpeg, webp, avif
    width = 1440,
    height = 900,
    fullPage = false,
    blockAds = true,               // Block advertisements
    blockCookies = true,           // Block cookie banners
    delay = 2000,                  // Wait 2s before capture
    devicePreset = "iPhone15",     // Mobile device emulation
    css = "body { background: white; }",
    responseType = "url"           // Get hosted URL
};

Get Hosted URL

var payload = new
{
    url = "https://example.com",
    format = "png",
    responseType = "url"
};

var content = new StringContent(
    JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json"
);

var response = await client.PostAsync("/screenshot", content);
var json = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<JsonElement>(json);

Console.WriteLine(result.GetProperty("url").GetString());
// → https://cdn.snapapi.pics/screenshots/abc123.png

Generate PDFs

var payload = new
{
    url = "https://example.com",
    format = "A4",
    printBackground = true,
    margin = new
    {
        top = "20mm",
        bottom = "20mm",
        left = "15mm",
        right = "15mm"
    }
};

var content = new StringContent(
    JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json"
);

var response = await client.PostAsync("/pdf", content);
var pdfBytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("page.pdf", pdfBytes);

PDF from HTML String

var payload = new
{
    html = "<h1>Invoice #1234</h1><p>Total: $99.00</p>",
    format = "A4",
    printBackground = true
};

Extract Content

var payload = new
{
    url = "https://example.com/blog-post",
    format = "markdown"
};

var content = new StringContent(
    JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json"
);

var response = await client.PostAsync("/extract", content);
var json = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<JsonElement>(json);

Console.WriteLine(result.GetProperty("markdown").GetString());

Record Videos

var payload = new
{
    url = "https://example.com",
    duration = 5,
    width = 1280,
    height = 720,
    format = "mp4"
};

var content = new StringContent(
    JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json"
);

var response = await client.PostAsync("/video", content);
var videoBytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("recording.mp4", videoBytes);

Error Handling

var response = await client.PostAsync("/screenshot", content);

switch ((int)response.StatusCode)
{
    case 200:
        var data = await response.Content.ReadAsByteArrayAsync();
        await File.WriteAllBytesAsync("screenshot.png", data);
        Console.WriteLine("Success!");
        break;
    case 401:
        Console.Error.WriteLine("Invalid API key");
        break;
    case 429:
        var retryAfter = response.Headers.RetryAfter?.Delta?.TotalSeconds;
        Console.Error.WriteLine($"Rate limited. Retry after {retryAfter}s");
        break;
    case 400:
        var error = await response.Content.ReadAsStringAsync();
        Console.Error.WriteLine($"Bad request: {error}");
        break;
    default:
        Console.Error.WriteLine($"Error {(int)response.StatusCode}");
        break;
}
⚠️ Rate Limits: Free tier: 200 requests/month. Starter: $19/mo. Pro: $79/mo. View plans.

ASP.NET Core Integration

using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/[controller]")]
public class ScreenshotController : ControllerBase
{
    private readonly HttpClient _httpClient;

    public ScreenshotController(IHttpClientFactory httpClientFactory)
    {
        _httpClient = httpClientFactory.CreateClient("SnapAPI");
    }

    [HttpGet]
    public async Task<IActionResult> Capture([FromQuery] string url)
    {
        var payload = new { url, format = "png", width = 1280 };

        var content = new StringContent(
            JsonSerializer.Serialize(payload),
            Encoding.UTF8,
            "application/json"
        );

        var response = await _httpClient.PostAsync("/screenshot", content);
        var imageBytes = await response.Content.ReadAsByteArrayAsync();

        return File(imageBytes, "image/png", "screenshot.png");
    }
}

Dependency Injection

Register a named HttpClient in Program.cs:

// Program.cs
builder.Services.AddHttpClient("SnapAPI", client =>
{
    client.BaseAddress = new Uri("https://api.snapapi.pics");
    client.DefaultRequestHeaders.Authorization =
        new AuthenticationHeaderValue("Bearer",
            builder.Configuration["SnapAPI:ApiKey"]);
});

// appsettings.json
{
    "SnapAPI": {
        "ApiKey": "YOUR_API_KEY"
    }
}

Next Steps