Many APIs let you authenticate with an API key or with both an API Key and an API secret.
For now, you can think of the API Key and API Secret like your username and password — except these values are programmatically generated.
In general, you should treat your API Key and API Secret like this:
- If you only have API Key:
- Treat the API key like your password.
- If you have both API key and secret:
- Treat your API key like your username
- Treat your API secret like your password
Using with API Key and API Secret
There’s no standard way (as far as I know) to authenticate with API Keys and API Secrets, so each provider implements the authentication process differently.
But they usually use one of these methods
- Send the API Key and Secret in an
Authorizationheader - Send the API Key and Secret through a query parameter
- Send the API key and secret in the
body
For example, Stripe lets you authenticate with an API key through an Authorization header that begins with Bearer — so Stripe’s API Key doubles up as an access token.
fetch('https://api.stripe.com/v1/charges', { headers: { Authorization: 'Bearer sk_test_KCm2E9suIKWjVpiKvS8OoSKl', },}) .then(r => r.json()) .then(console.log) .catch(console.log)
Another example, Convertkit, an email newsletter platform, ask you to send in an API key as a query parameter.
Using API Keys and Secrets in SDKs
Some apps may provide you with libraries that make their API easier to use. These libraries are usually called Software Development Kits (SDK).
In these SDKs, they may ask you to enter the API or Secret directly into the function they provide you with. For example, Stripe does this.
import Stripe from 'stripe'const stripe = new Stripe('YOUR_API_KEY')That’s all you need to know about authenticating with API Keys and Secrets for now!
Next, let’s talk about one important thing that trips up most developers when using Rest APIs — CORS.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson