Authentication
Choose a personal API key or OAuth 2.1 with PKCE.
Use a personal API key for your own scripts. Use OAuth for an application acting for other NinjaBuy customers, including MCP clients.
OAuth 2.1
Use the authorization code flow with PKCE S256. REST and MCP accept the same NinjaBuy OAuth access token. Each token acts for the account that signed in and approved access; you cannot select another account in the request body.
All OAuth endpoints use the origin https://www.ninjabuy.com. Discovery and authorization paths:
OAuth discovery · MCP resource discovery
POST /api/auth/mcp/register
GET /api/auth/mcp/authorize
POST /api/auth/mcp/token- Register your client at
POST /api/auth/mcp/register. A public client sends JSON withclient_name,redirect_urisandtoken_endpoint_auth_method: "none". Keep the returnedclient_id. - Generate a fresh random
stateand PKCEcode_verifier. Derive the base64url-encoded SHA-256code_challenge. - Open
/api/auth/mcp/authorizein the user's browser withresponse_type=code,client_id,redirect_uri,state,code_challenge,code_challenge_method=S256andscope=openid offline_access. - The user signs in with their NinjaBuy phone number and approves your application. At your callback, verify
statebefore using the returnedcode. - Send a form-encoded POST to
/api/auth/mcp/tokenwithgrant_type=authorization_code,code,client_id, the sameredirect_uriandcode_verifier.
Send the resulting access token on REST requests:
Authorization: Bearer YOUR_ACCESS_TOKENAccess tokens last 1 hour. With offline_access, refresh tokens allow renewal for up to 7 days. Refresh through the same token endpoint using a form-encoded POST with grant_type=refresh_token, client_id and refresh_token. Store tokens securely and sign in again when renewal is no longer valid.
A missing, expired or invalid access token returns 401 with a WWW-Authenticate discovery challenge. See Errors.