Go
The official Go client library for the VPNDetection API.
Getting Started
go get github.com/vpndetection-io/sdk-go/v5Requires Go 1.26 or newer. The module path ends in sdk-go, but the package it declares is vpndetection:
import vpndetection "github.com/vpndetection-io/sdk-go/v5"Usage
No API key needed to start. Without a key, a lookup answers ip and is_vpn, up to 1000 a day per source address.
client, err := vpndetection.New()
if err != nil {
log.Fatal(err)
}
result, err := client.Lookup(ctx, "45.83.91.1")
if err != nil {
log.Fatal(err)
}
fmt.Println(result.IsVpn) // trueWith an API key
A free key adds Tor and relay detection, where the address is and the AS that announces it. Every paid plan answers every field, the same on each, and the plans differ only in how many lookups they include. Create a key in the console, then pass it in:
client, err := vpndetection.New(vpndetection.WithAPIKey(os.Getenv("VPNDETECTION_API_KEY")))
result, err := client.Lookup(ctx, "45.83.91.1")
fmt.Println(result.IsVpn) // true
if loc := result.Location; loc != nil {
fmt.Println(loc.Country) // the ISO 3166 country code
}
if asn := result.Asn; asn != nil {
fmt.Println(asn.Name) // who runs the announcing network
}
fmt.Println(vpndetection.BoolValue(result.IsHosting)) // true, on a paid plan
if vpn := result.Vpn; vpn != nil && vpn.Provider != nil {
fmt.Println(*vpn.Provider) // nordvpn
}Your own address
result, err := client.MyIP(ctx)
fmt.Println(result.IP) // the address we saw this call come fromSame answer Lookup would give for that address, and the same cost against your allowance. It is deliberately not cached: which address you are is the whole question, and a machine that moves between networks would otherwise be told where it used to be.
Your plan and usage
acct, err := client.MyEntitlement(ctx)
fmt.Println(acct.Plan.Key) // max
fmt.Println(acct.Usage.Requests) // 580
fmt.Println(acct.Usage.WindowEnd) // when the allowance resetsUsage is counted against the anniversary of your subscription, not the calendar month and not the billing period, and it is the same number a lookup is gated on. HardLimit is nil on an uncapped plan, which is not the same as zero.
Batch lookup
Look up many addresses at once. Bogons and cached answers are handled locally, and everything else goes to the batch endpoint in chunks of up to 1000 addresses, in parallel:
results, err := client.LookupBatch(ctx, []string{"45.83.91.1", "8.8.8.8", "1.1.1.1"})
for ip, result := range results {
if result.Err != nil {
fmt.Printf("%s: %v\n", ip, result.Err)
continue
}
fmt.Printf("%s: %v\n", ip, result.Result.IsVpn)
}Results are keyed by address, so duplicates in your list collapse into a single entry and one address failing never loses the rest: it carries its error as its value, with the status the API would have given that address on its own.
Pass as many addresses as you like; splitting them into chunks is the library's job. How many chunks are in flight at once, how many times a failed chunk is retried, and how long each attempt may take are configurable per call:
results, err := client.LookupBatch(ctx, manyIPs,
vpndetection.Concurrency(4), vpndetection.Retries(4), vpndetection.Timeout(10*time.Second))Retries and Timeout work on Lookup, MyIP and MyEntitlement too. The timeout bounds each attempt rather than the whole call, and defaults to the HTTP client's: 30 seconds unless WithHTTPClient supplied another.
Caching
Answers are cached by default, so repeat lookups of the same address are free:
client, err := vpndetection.New()
result, err := client.Lookup(ctx, "45.83.91.1")
fmt.Println(result.IsVpn) // true, API request
result2, err := client.Lookup(ctx, "45.83.91.1")
fmt.Println(result2.IsVpn) // true, no API request, result was cachedYou can change the default cache variables (max size, TTL, etc) on initialization, or even disable it:
client, err := vpndetection.New(vpndetection.WithCache(50_000, 6*time.Hour))
clientNoCache, err := vpndetection.New(vpndetection.WithoutCache())Private and reserved addresses
Private, loopback, link-local, documentation and multicast addresses (and their IPv6 equivalents, including the 6to4 and Teredo ranges) can never be VPN or proxy infrastructure. The library answers them locally, so they cost no request and no quota:
result, err := client.Lookup(ctx, "192.168.1.1")
result.IsBogon // true, this answer was computed rather than served
result.IsVpn // falseBecause the answer is computed rather than served, it always carries every field, whatever plan you are on. Do not read your plan's shape off a private address.
The check is available on the client, which is handy when your inputs are addresses anyway:
client.IsBogon("10.0.0.1") // true
client.IsBogon("8.8.8.8") // falseIt is also callable on its own, if you want it without a client:
vpndetection.IsBogon("10.0.0.1") // trueErrors
Failures return a *vpndetection.Error carrying a Kind and a Retryable flag:
result, err := client.Lookup(ctx, "1.1.1.1")
var apiErr *vpndetection.Error
if errors.As(err, &apiErr) {
fmt.Println(apiErr.Kind, apiErr.Retryable())
}Kind is one of bad_request, unauthorized, forbidden, rate_limited, quota_exceeded, server_error or network.
Note that rate_limited and quota_exceeded both arrive as HTTP 429 and are not the same thing. A rate limit is when the API faces extreme traffic bursts and so retrying later works; but a spent quota needs your allowance raised or the window to roll over. The library retries rate limits for you, but not if your quota is exceeded.
Database downloads
If your key carries the db.download scope, the licensed databases are available through client.Database. DownloadFile fetches one to a path, streaming it straight to disk so that nothing bigger than a chunk is ever held in memory:
databases, err := client.Database.List(ctx)
written, err := client.Database.DownloadFile(ctx,
"vpn_ip_extended_v1", vpndetection.FormatMMDB, "./vpn_ip_extended_v1.mmdb")
fmt.Printf("%d bytes\n", written)Or stream it into a writer of your own, take the time-limited link and run the transfer yourself, or take a small database as bytes:
written, err := client.Database.Download(ctx, "vpn_ip_extended_v1", vpndetection.FormatMMDB, w)
url, err := client.Database.DownloadURL(ctx, "vpn_ip_extended_v1", vpndetection.FormatMMDB)
raw, err := client.Database.DownloadBytes(ctx, "cdn_ip_v1", vpndetection.FormatCSVGZ)DownloadBytes holds the whole file in memory, and the catalog runs from cdn_ip_v1 at 10 KB to resproxy_ip_90d_v1 at 1.79 GB, so use DownloadFile for anything you have not measured.
Format is a defined string type, so Format("zip") compiles. Anything the API does not publish is refused before the request leaves, as a bad_request naming what is allowed; Valid is the same check for a format you take from a flag or a config file:
if !vpndetection.Format(fromFlag).Valid() {
// csvgz or mmdb
}Sign in with OAuth (device flow)
A program running on the person's own machine can let them sign in with a browser and pick one of their API keys, instead of asking them to paste it:
client, err := vpndetection.New()
device, err := client.Oauth.DeviceAuthorization(ctx, "your-client-id",
vpndetection.DeviceAuthorizationOptions{Scope: "account.read apikeys.read apikeys.reveal"})
fmt.Printf("Open %s and enter %s\n", device.VerificationURI, device.UserCode)
token, err := client.Oauth.PollDeviceToken(ctx, "your-client-id", device)
if token.Apikey == nil {
log.Fatal("no API key came back: none was picked, or it cannot be revealed")
}
keyed, err := vpndetection.New(vpndetection.WithAPIKey(*token.Apikey))A denied sign-in satisfies errors.Is(err, vpndetection.ErrOauthAccessDenied) and a code that ran out errors.Is(err, vpndetection.ErrOauthExpiredToken), and client IDs are issued on request from support@vpndetection.io. client.Oauth.Revoke(ctx, "your-client-id", *token.RefreshToken) signs the machine out again.
Sign in with OAuth (authorization code)
An app that can take a browser redirect signs the person in there instead, with a PKCE pair made for that one sign-in:
client, err := vpndetection.New()
redirectURI := "http://127.0.0.1:8765/callback"
pkce := client.Oauth.CreatePkce()
authURL, err := client.Oauth.AuthorizationURL("your-client-id", redirectURI, pkce.Challenge,
vpndetection.AuthorizationURLOptions{Scope: "apikeys.use", State: "your-state"})
// Open authURL in the browser. Its redirect to redirectURI carries code and state.
token, err := client.Oauth.ExchangeAuthorizationCode(ctx, "your-client-id", code, pkce.Verifier, redirectURI)Check that state came back as you sent it before you exchange code, which works once. The client ID can also be the https URL of a client metadata document your app serves, and such an app is never handed a key, so token.Apikey stays nil.
Register a client
An app with no client ID, and no client metadata document to use as one, registers itself, then signs in with the ClientID it gets back as the client ID for AuthorizationURL and ExchangeAuthorizationCode:
client, err := vpndetection.New()
registration, err := client.Oauth.Register(ctx, []string{"http://127.0.0.1:8765/callback"},
vpndetection.RegisterOptions{ClientName: "Your App"})Each redirect URI must be https on a public host name, or http on a loopback address, which then matches any port. Nothing is kept until a person approves the client, so an app with no client ID saved can simply register again.
Absent is not false
Every field beyond IP and IsVpn is a pointer, because your plan decides which of them the API sends. A nil pointer means "not in your plan", not "checked, and no".
vpndetection.BoolValue(result.IsHosting) // when you only want the flag
result.IsHosting == nil // not in your plan