Skip to content

C#

This C# example shows how to use the Vaxtor Cloud ALPR API to request a plate analysis that includes Make, Model, and Color (MMC).

Code sample

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

class Program
{
    static async Task Main()
    {
        string tokenUrl = "https://auth.vaxtor.cloud/connect/token";
        string apiUrl   = "https://vaxtor.cloud/analysis/api/plate";

        string clientId     = "PUT_YOUR_CLIENT_ID_HERE";
        string clientSecret = "PUT_YOUR_CLIENT_SECRET_HERE";
        string scope        = "api";

        // Optional: OCR region override
        // string ocrRegion = "europe";

        using HttpClient client = new HttpClient();

        // ---- Get Token ----
        var tokenResponse = await client.PostAsync(
            tokenUrl,
            new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string,string>("grant_type","client_credentials"),
                new KeyValuePair<string,string>("client_id",clientId),
                new KeyValuePair<string,string>("client_secret",clientSecret),
                new KeyValuePair<string,string>("scope",scope),
            })
        ).ConfigureAwait(false);

        string tokenJson = await tokenResponse.Content.ReadAsStringAsync();
        string accessToken = JsonSerializer.Deserialize<JsonElement>(tokenJson)
            .GetProperty("access_token")
            .GetString();

        if (string.IsNullOrEmpty(accessToken))
        {
            Console.WriteLine("Error getting token");
            return;
        }

        // ---- Prepare request ----
        client.DefaultRequestHeaders.Authorization =
            new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);

        // Optional OCR Region
        // if (!string.IsNullOrEmpty(ocrRegion))
        //     client.DefaultRequestHeaders.Add("Vaxtor-Ocr-Region", ocrRegion);

        // ---- Send image as application/octet-stream ----
        await using var fileStream = File.OpenRead("image.jpg");
        var content = new StreamContent(fileStream);
        content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");

        var response = await client.PostAsync(apiUrl, content).ConfigureAwait(false);

        // ---- Show Response ----
        Console.WriteLine(await response.Content.ReadAsStringAsync());
    }
}

Code Overview

  1. Authentication

The script retrieves a bearer token via OAuth2 Client Credentials at https://auth.vaxtor.cloud/connect/token.

Export your client-id and client-secret before running the script:

powershell
$env:VAXTOR_CLIENT_ID="your-client-id"
$env:VAXTOR_CLIENT_SECRET="your-client-secret"
  1. API Request

Uses an HttpClient to POST the image file to https://vaxtor.cloud/analysis/api/plate with the Authorization: Bearer <token> header.

Vaxtor Technologies