Connect your app to GunSpec
For an app that acts for other people: a scheduler, a CMS, an agent platform. Each person connects their own GunSpec account once, and your app reads the catalogue as them, at their plan and under their limits, with no API key changing hands.
Registering an app
Apps are registered by GunSpec, one for each environment you run. Write to us with your app's name, its homepage and every redirect address it will use.
- You receive a client id (
gsapp_) and a client secret (gscs_). The secret is shown once and kept by us only as a hash: store it like any other server secret, never in a browser or a mobile app. - Redirect addresses are matched exactly, character for character, so register each one you use: production, a preview environment, and a loopback address for development. Production accepts https only.
- A lost or leaked secret is replaced, not recovered. The old one stops working when the new one is issued; your connected people stay connected.
The OAuth 2.1 flow: authorize, consent, token
Authorization code with PKCE, as OAuth 2.1 requires. The server describes itself at /.well-known/oauth-authorization-server, so a generic OAuth client needs only the issuer.
import { authorizeUrl, createPkce, parseRedirect, exchangeCode } from '@buun_group/gunspec-sdk/oauth' // 1. Send the person to GunSpec. Keep the verifier and the state// on your server, bound to this browser (a __Host- cookie).const pkce = await createPkce()const { url, state } = authorizeUrl({ clientId: process.env.GUNSPEC_CLIENT_ID, redirectUri: 'https://app.example.com/integrations/gunspec/callback', scopes: ['catalog.read'], codeChallenge: pkce.challenge,}) // 2. On the callback: check the state and the issuer, then trade the code.const code = parseRedirect(request.url, state)const tokens = await exchangeCode({ clientId: process.env.GUNSPEC_CLIENT_ID, clientSecret: process.env.GUNSPEC_CLIENT_SECRET, code, redirectUri: 'https://app.example.com/integrations/gunspec/callback', codeVerifier: pkce.verifier,})// tokens.accessToken (gsat_, one hour), tokens.refreshToken (gsrt_, rotates)- Send the person to
GET /oauth/authorizewithresponse_type=code, yourclient_id, a registeredredirect_uri,scope, astateand an S256code_challenge. State and PKCE are both required. - They sign in to GunSpec if they need to and see your app's name and what it may do, then choose Allow or Deny. Deny returns
error=access_deniedto your redirect address. - Allow returns
code, yourstateandiss. Check that the state is the one this browser was given and thatissis the GunSpec API origin before you use the code. - Trade the code at
POST /oauth/tokenwithin ten minutes, authenticating with your client id and secret (HTTP Basic, or in the form). A code works once. - An unknown client or an unregistered redirect address never redirects: the person sees an error page at GunSpec, so a mistyped address cannot leak a code.
Scopes
One scope today, and it is the catalogue.
catalog.readreads what the person's own key would read on/v1: firearms, specifications, calibers, media. It does not reach their account, keys, billing or favorites.- The one exception is your own webhooks: a token may manage the endpoints your app made for that person, and nothing else under
/v1/me.
Access tokens and refresh tokens
Call /v1 with the access token as a Bearer token. The request runs at the person's plan: a Builder account's token passes the Builder gate, and its calls count against their allowance.
import { GunSpec } from '@buun_group/gunspec-sdk' // The person's access token, never an API key: the SDK sends it as Bearer.const gunspec = new GunSpec({ accessToken: tokens.accessToken }) const media = await gunspec.firearms.listMedia('glock-g17-gen5', { republish: 'free' })// Every item here may be posted with no credit line.import { refreshToken, GunSpecOAuthError } from '@buun_group/gunspec-sdk/oauth' try { const next = await refreshToken({ clientId, clientSecret, refreshToken: stored.refreshToken }) await save(next) // the old refresh token is now spent: store the new one before using it} catch (error) { if (error instanceof GunSpecOAuthError && error.error === 'invalid_grant') { // The person disconnected, or a spent token was presented again. // The connection is over: ask them to connect again. }}- Access tokens (
gsat_) last one hour. Refresh a minute or so before expiry rather than on the first 401. - Refresh tokens (
gsrt_) last ninety days and rotate: every refresh returns a new one and spends the old. Store the new token before you use it, and refresh under a lock, because presenting a spent refresh token ends the whole connection. - A token for a connection that has ended answers 401 with reason
KEY_INVALID. The person disconnected your app, or deleted their account: ask them to connect again. - On disconnect in your app, call
POST /oauth/revokewith the refresh token. That ends the connection at GunSpec too, with every token and webhook it holds. - Never put an access token where an API key goes in a public place either. Keys in production applies to tokens just the same.
Rights on media: what you may post
Every media item carries a rights block, derived from its recorded source, author and licence. Decide what to post from rights.republish, never by matching the credit strings, which are for showing a credit line.
| rights.republish | What it allows |
|---|---|
free | Post it with no credit line. Our own renders, silhouettes and drawings, and anything in the public domain or under CC0. |
with_credit | Post it only with the source, author and licence from credit beside it. A CC BY family licence. |
no | Do not post it. A NonCommercial licence, a licence we do not recognise, or none recorded. |
rights.ownersays whose it is (gunspec,third_partyorunknown) andrights.licensethe licence as recorded.GET /v1/firearms/{id}/media?republish=freelists only what needs no credit line.- Silhouettes and line art are SVG, which no social platform takes. Their items carry a
pngblock of URLs for a PNG copy at each size, on a white ground unless you ask forbackground=transparent. See Firearm media.
Webhooks for apps
Your app can subscribe on a person's behalf, so an automation runs when something changes instead of on a timer. Call the webhooks endpoint with their access token.
curl -sS -X POST https://api.gunspec.io/v1/me/webhooks \ -H "Authorization: Bearer $GUNSPEC_ACCESS_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "url": "https://app.example.com/hooks/gunspec", "events": ["media.created", "firearm.updated", "firearm.created"], "firearmIds": ["glock-g17-gen5", "sig-sauer-p320"] }'- Three events:
firearm.createdfor a new firearm,firearm.updatedfor a changed specification, andmedia.createdfor a new picture, which carries the media item with itsrights. firearmIdsnarrowsfirearm.updatedandmedia.createdto the firearms a workspace follows.firearm.createdalways arrives, since nobody can follow a firearm that did not exist.- App webhooks work from the Builder plan up. Five endpoints for each connection.
- Your app sees only the endpoints it made for that connection. The person sees them in their account too, marked with your app, and every one is deleted when the connection ends.
- Deliveries are signed exactly as every GunSpec webhook is: verify
X-Webhook-Signatureand dedupe onX-Webhook-Id. See Mirror the catalog for a receiver.
Account details and limits
Show the connected account on your connection panel from GET /oauth/userinfo: who it is, their plan, and what is left of their allowance.
curl -sS https://api.gunspec.io/oauth/userinfo -H "Authorization: Bearer $GUNSPEC_ACCESS_TOKEN" { "sub": "usr_...", "email": "person@example.com", "name": "A. Person", "plan": "builder", "usage": { "plan": "builder", "limits": { "daily": 5000, "monthly": 25000 }, "remaining": { "daily": 4870, "monthly": 21342 }, "mcpDaily": 500 }}usage.limitsare the plan's daily and monthly request limits andusage.remainingwhat is left of each now. A limit the plan does not have is null.usage.mcpDailyis the daily limit for MCP tool calls.- Every
/v1answer to a Bearer call carries the sameX-Daily-*andX-Monthly-*headers as a call with a key, so you can pace against them without asking again. - Reading userinfo spends nothing. See Rate limits for how the limits work.