BIN lookup with C#

This page uses .NET and HttpClient. Create an API key in the quickstart. Read it from the environment variable BINLOOKUP_API_KEY.

Look up one BIN

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

using var client = new HttpClient();
var apiKey = Environment.GetEnvironmentVariable("BINLOOKUP_API_KEY");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);

var body = new StringContent(
    JsonSerializer.Serialize(new { number = 42467101 }),
    Encoding.UTF8,
    "application/json"
);

var response = await client.PostAsync("https://api.binlookupapi.com/v1/bin", body);
var payload = await response.Content.ReadAsStringAsync();

switch ((int)response.StatusCode)
{
    case 200:
        Console.WriteLine(payload);
        Console.WriteLine(response.Headers.GetValues("X-Quota-Remaining").FirstOrDefault());
        break;
    case 401:
        Console.Error.WriteLine("The API key is missing or not valid.");
        break;
    case 403:
        Console.Error.WriteLine(payload);
        break;
    case 404:
        Console.Error.WriteLine("The BIN is not in the data.");
        break;
    case 429:
        Console.Error.WriteLine("The daily quota is used.");
        break;
    case 400:
        Console.Error.WriteLine(payload);
        break;
    default:
        Console.Error.WriteLine($"The API returned HTTP {(int)response.StatusCode}.");
        break;
}

Parse data from the JSON. The fields are in the API reference. On BLAPI-150KPM and above, the body also contains meta.longer_bin_available.

HTTP 403 is FORBIDDEN or FEATURE_NOT_AVAILABLE. Read the error property.

Look up many BINs

POST /v1/bins accepts 1 to 2,000 values. It requires BLAPI-500KPM, or a test-mode organisation. Each value uses one lookup.

var body = new StringContent(
    JsonSerializer.Serialize(new { numbers = new object[] { 400062, "400597", 99999999 } }),
    Encoding.UTF8,
    "application/json"
);

var response = await client.PostAsync("https://api.binlookupapi.com/v1/bins", body);
var payload = await response.Content.ReadAsStringAsync();

if ((int)response.StatusCode == 403 && payload.Contains("FEATURE_NOT_AVAILABLE"))
{
    Console.Error.WriteLine("Batch lookup needs the BLAPI-500KPM plan.");
}
else if (response.IsSuccessStatusCode)
{
    Console.WriteLine(payload);
}

The results array keeps your order. See Batch lookups. A batch that does not fit in the remaining quota returns 429 and uses no quota.