BIN lookup with Go

This page uses the Go standard library. Create an API key in the quickstart. Read it from the environment variable BINLOOKUP_API_KEY.

Look up one BIN

package main

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
	"os"
	"time"
)

func main() {
	body := bytes.NewBufferString(`{"number":42467101}`)
	req, err := http.NewRequest(http.MethodPost, "https://api.binlookupapi.com/v1/bin", body)
	if err != nil {
		panic(err)
	}
	req.Header.Set("Authorization", "Bearer "+os.Getenv("BINLOOKUP_API_KEY"))
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{Timeout: 30 * time.Second}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	payload, _ := io.ReadAll(resp.Body)
	switch resp.StatusCode {
	case 200:
		fmt.Println(string(payload))
		fmt.Println(resp.Header.Get("X-Quota-Remaining"))
	case 401:
		fmt.Fprintln(os.Stderr, "The API key is missing or not valid.")
	case 403:
		fmt.Fprintln(os.Stderr, string(payload))
	case 404:
		fmt.Fprintln(os.Stderr, "The BIN is not in the data.")
	case 429:
		fmt.Fprintln(os.Stderr, "The daily quota is used.")
	case 400:
		fmt.Fprintln(os.Stderr, string(payload))
	default:
		fmt.Fprintf(os.Stderr, "The API returned HTTP %d.\n", resp.StatusCode)
	}
}

The success JSON has mode and data. 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 field.

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.

client := &http.Client{Timeout: 60 * time.Second}
body := bytes.NewBufferString(`{"numbers":[400062,"400597",99999999]}`)
req, err := http.NewRequest(http.MethodPost, "https://api.binlookupapi.com/v1/bins", body)
if err != nil {
	panic(err)
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("BINLOOKUP_API_KEY"))
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
if err != nil {
	panic(err)
}
defer resp.Body.Close()
payload, _ := io.ReadAll(resp.Body)

if resp.StatusCode == 403 && bytes.Contains(payload, []byte("FEATURE_NOT_AVAILABLE")) {
	fmt.Fprintln(os.Stderr, "Batch lookup needs the BLAPI-500KPM plan.")
	return
}
if resp.StatusCode == 200 {
	fmt.Println(string(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.