Authentication
To ensure the integrity and security of all data exchanges, 4Geeks implements API Key Authentication as the primary architectural standard for authorizing inbound service requests. Under this protocol, every programmatic interaction must be accompanied by a unique, valid API Key; failure to provide this credential will result in an immediate authorization denial to protect sensitive system resources. These credentials are submitted utilizing the HTTP Basic Authentication framework, as strictly defined by the RFC 7617 industry standard, which requires the key to be passed within the request header according to the specific formatting conventions outlined in the following technical documentation.
Where <base64_encoded_credentials> is your API Key followed by a colon (:) and then Base64 encoded.
Step-by-Step Encoding¶
1. Your API Key:
2. Add colon after the key:
3. Encode to Base64:
4. Add to Authorization header:
Code Examples¶
import { HttpClient, HttpHeaders } from "@angular/common/http";
export class CustomerService {
private apiKey = "sk_test_51O62xYzAbcDef123";
constructor(private http: HttpClient) {}
getCustomers() {
const headers = new HttpHeaders({
Authorization: `Api-Key ${this.apiKey}`,
});
return this.http.get("http://api.4geeks.io/v1/customers/", { headers });
}
}
Error Handling¶
400 Bad Request¶
Occurs when the request is missing required fields or has invalid format.
Example:
{
"message": {
"code": 400,
"title": "Invalid request",
"content": "Please provide a valid 'type' field: 'test' or 'live'.",
"type": "danger"
}
}
Causes:
- Missing
typefield in PATCH request - Invalid
typevalue (must be"test"or"live")
Solution: Include {"type": "test"} or {"type": "live"} in request body.
401 Unauthorized¶
Authentication failed. API Key is invalid, missing, or malformed.
Examples:
- Missing Authorization Header (Bearer Token)
Request: GET /v1/auth/api-keys/ (without Authorization header)
Response:
{
"detail": "Authentication credentials were not provided."
}
- Key not Found
Request: Authorization header with non-existent key
Response:
{
"detail": "Given token not valid for any token type",
"code": "token_not_valid",
"messages": [
{
"token_class": "AccessToken",
"token_type": "access",
"message": "Token is invalid or expired"
}
]
}
Solutions:
- Verify you included the
Authorization: Bearer <Bearer_Token>header - Verify the key hasn’t been rotated recently
- Test with Postman to debug the issue
404 Not Found¶
No API keys exist for this user.
Example:
{
"code": 404,
"title": "API Keys not found",
"content": "No API keys found for this user. Please contact support.",
"type": "warning"
}
Solution: Contact support to initialize API keys for your account.
Security Best Practices¶
Keep Your Keys Safe
- Store keys in environment variables, never in code
- Don’t commit keys to version control
- Use
.envfiles with.gitignore - Rotate keys every 3-6 months
- Regenerate immediately if exposed
Environment Variable Example:
# .env
API_KEY_TEST=sk_test_51O62xYzAbcDef123
API_KEY_LIVE=sk_live_xYzAbcDef123456789
API_URL=http://api.4geeks.io
Application Usage:
Troubleshooting¶
Problem: 401 Unauthorized “Invalid API Key format”¶
Causes:
- API Key is incomplete or truncated
- Base64 encoding doesn’t include the colon (
:) - Extra spaces in the key or header
- Key was regenerated recently
Solutions:
- Copy the key again:
- Go to dashboard Settings → API Keys
- Copy the full key carefully
-
Verify Base64 encoding:
- Use an online tool: https://www.base64encode.org/
- Your key should end with
: - Example:
sk_test_xyz:(with colon)
-
Test with Postman:
- Create new request
- Authorization → Basic Auth
- Username: [your full key]
- Password: [leave empty]
- Send
Problem: Key stopped working after rotation¶
Cause: You rotated the key but your application still uses the old one.
Solution:
- Go to Settings → API Keys
- Copy the new key
- Update your
.envor configuration - Restart your application
Problem: 404 “API Keys not found”¶
Cause: No API keys are initialized for your account.
Solution: Contact supports to initialize your API keys.
Frequently Asked Questions (FAQ)¶
Q: Can I have multiple API Keys? A: No. You have exactly one Test Key and one Live Key. Use the rotation endpoint to regenerate them.
Q: What happens if I expose my Live Key? A: Rotate it immediately from the dashboard. The exposed key becomes invalid within 1 minute.
Q: Can I use the same key for Test and Live? A: No. Test and Live keys are separate. Always use Test Keys in development.
Q: Do I need to refresh my API Key? A: Unlike JWT tokens, API Keys don’t expire. They remain valid until you rotate them.
Q: Can I use API Keys with webhooks? A: No. Webhooks are delivered by our servers to your endpoint. You authenticate the webhook using signatures, not API Keys.
- Ainda tem dúvidas? Pergunte na comunidade..
- Consulte el changelog.