API keys overview
Manage your APIs
This guide will walk you through setting up API key management for your APIs using Kinde. You’ll learn how to register your APIs, create API keys, and implement key verification in your application.
Before you following this guide, ensure you have:
Before you can issue API keys, you need to register your API in Kinde:
You’ll see an API ID
for your new API. This ID is used when creating API keys.
You can enable your customers to create API keys via the self-service portal or via API, but for this guide we’ll assume you are creating them on their behalf inside the Kinde admin area.
Securely issue this API key to your customer.
Authorization
header.Authorization
header.// Extract API key from requestconst apiKey = req.headers.authorization?.replace("Bearer ", "");
// Verify with Kinde (requires M2M access token)// Obtain an M2M token as per /developer-tools/kinde-api/connect-to-kinde-api/const verificationResult = await verifyApiKey(apiKey, m2mAccessToken);
if (verificationResult.is_valid) { // Key is valid, check scopes if (verificationResult.scopes.includes("read:users")) { // Proceed with request res.json({data: "Your data here"}); } else { res.status(403).json({error: "Insufficient scope"}); }} else { res.status(401).json({error: "Invalid API key"});}
async function verifyApiKey(apiKey, m2mAccessToken) { const response = await fetch("https://your-domain.kinde.com/api/v1/api_keys/verify", { method: "POST", headers: { "Content-Type": "application/json", "Authorization": `Bearer ${m2mAccessToken}` }, body: JSON.stringify({ api_key: apiKey }) });
return response.json();}
The response will look like this:
{ "code": "API_KEY_VERIFIED", "message": "API key verified", "is_valid": true, "key_id": "api_key_0195ac80a14e8d71f42b98e75d3c61ad", "status": "active", "scopes": ["read:users", "write:users"], "org_code": "org_1234567890", "user_id": "kp_1234567890", "last_verified_on": "2024-11-18T13:32:03+11", "verification_count": 42}
That’s it! You have verified the API key and can proceed with the request.