Quick Start
Generate a QR code in C# using HttpClient:
C# - Basic Example
using System.Net.Http;
using System.Text;
using System.Text.Json;
public class QRCodeService
{
private readonly HttpClient _httpClient;
private readonly string _apiKey;
public QRCodeService(string apiKey)
{
_httpClient = new HttpClient();
_apiKey = apiKey;
}
public async Task<byte[]> GenerateAsync(string data, int size = 300)
{
var request = new
{
data = data,
size = size,
format = "png"
};
var json = JsonSerializer.Serialize(request);
var content = new StringContent(json, Encoding.UTF8, "application/json");
_httpClient.DefaultRequestHeaders.Clear();
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_apiKey}");
var response = await _httpClient.PostAsync(
"https://www.qrcodeapi.io/api/generate", content);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsByteArrayAsync();
}
}
ASP.NET Core Controller
QRController.cs
[ApiController]
[Route("api/[controller]")]
public class QRController : ControllerBase
{
private readonly QRCodeService _qrService;
public QRController(IConfiguration config)
{
_qrService = new QRCodeService(config["QRApi:Key"]);
}
[HttpGet]
public async Task<IActionResult> Get([FromQuery] string url)
{
var qrCode = await _qrService.GenerateAsync(url);
return File(qrCode, "image/png");
}
}
Dependency Injection Setup
Program.cs
// Add to your services
builder.Services.AddHttpClient<QRCodeService>();
builder.Services.AddScoped<QRCodeService>(sp =>
{
var config = sp.GetRequiredService<IConfiguration>();
return new QRCodeService(config["QRApi:Key"]);
});
Ready to Integrate with C#?
Get your free API key and start generating QR codes in your .NET application.
Get Your Free API Key