API Authentication: OAuth vs API key vs Basic Auth
Use of API requires authentication. API key is the simplest: one key that you send in every request. OAuth 2.0 is safer but more complicated: you get a token that gets older. Basic Auth has a username + password for every request. For most SME integrations, an API key is enough.
Why is Authentication Needed?
API is the "backdoor" of the service that programs use. Without authentication, anyone could read or change your data. Authentication ensures:
- Who is asking: Is the request from the right
- What to do: Read, write, delete?
- Restrictions: How many requests are allowed (rate limit)
OAuth vs API Key: Comparison
| Kill | Security | Complexity | Use case |
|---|---|---|---|
| API Key | Moderate | Easy | Server-server |
| OAuth 2.0 | High | Complex | Acting on behalf of the user |
| Basic Auth | Low | Easy | Old systems |
API Key
API key is a long random string that acts as a "password". You get it in the service settings and send it in every request.
# Headerissa (yleisempi)
Authorization: Bearer sk_live_abc123xyz
# Tai query-parametrina (ei suositella)
https://api.example.com/data?api_key=sk_live_abc123xyz
Pros
- Easy to implement and test
- No complicated token control
- Enough for most server-server integrations
Cons
- If the key leaks, access to the entire account
- The key does not automatically expire
- Not suitable for acting on behalf of the user
OAuth 2.0
OAuth is a standard in which the user gives permission for the application to work on its behalf. Example: "Allow the app to read your Google Calendar."
Typical OAuth-flow (Authorization Code)
- The user is redirected to the service's login page
- The user logs in and accepts the rights
- Service returns authorization code
- The application switches the code to access tokens
- Access tokens are used for API invitations
- Token is getting older → refresh token is used to get a new one
Pros
- The user password does not go to the application
- Limited rights (scope:t)
- Token is getting older → Reduced risk of damage
Cons
- More complex to implement
- Requires token management (refresh)
- Overkill if not required to act on behalf of the user
Basic Auth
Basic Auth sends a username and password in every request, Base64-encoded.
# Header
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
# Base64("username:password")
Pros
- Very easy to implement
- Widely Supported
Cons
- Base64 is not encryption – Requires HTTPS
- Password moves in every request
- Outdated practice, preferably use API key
What fits your integration?
- Server-server (e.g. CRM → billing): API key is enough
- On behalf of the user (e.g. Google Calendar): OAuth 2.0
- An old system that doesn't support anything else: Basic Auth + HTTPS
Safe storage of keys
- Not in code: Never commit keys to Git
- Environment Variables: .env file not commited
- Secrets manager: AWS Secrets Manager, Azure Key Vault
- Restrict permissions: Only required functions, no admin privileges
- Recycle regularly: Change keys at least annually
Common Errors
- Key on GitHub: Bots scan and use the leaked keys in minutes.
- HTTP without S: The key moves in plain language online.
- Too extensive rights: "Full access" when enough "read only".
- Not renewed: The same key used for years, although the employees changed.
- Not Monitored: You don't know if your key is used to make strange invitations.
Frequently asked questions
Where can I get an API key?
Service settings or developer portal. Frequently: Regulations → API / Integrations → Create a key. Some services require a separate request.
What do we do if the key leaks?
Deactivate the key immediately in the service settings. Create a new key and upgrade to integration. Check the logs to see what's been done with the key.
Can OAuth be used without user login?
Yes, OAuth's "Client Credentials" flow is designed for server-to-server use. But most often API key is a simpler option for the same need.
Read also
- Webhooks in Practice
- The Integration Project Speci
- Basic security package
- Integrations and automation as a service →
Need help with API integration?
We implement integrations safely and in a documented manner. Tell me what you want to combine.
Request an estimateWhat would you like to improve in your business?
Once an API identifies the caller, it still needs to verify the customer and the requested action on the record. Why Authentication Alone Does Not Make an Application Secure · AI code audit and production deployment
Datastorm Oy's ERP delivery is a written agreement, ready-to-use system from design to commissioning. The initial discussion and the offer are free of charge; any separate specification work will be agreed in advance. Maintenance and subsequent further development will be agreed separately. Explore our ERP service.
Learn about the content of the related service and Tell us about your business need.