Skip to content

API Key Security

Understanding the security model behind API keys, why they’re used, potential risks, and how the system mitigates threats.


API keys provide a balance between security and usability for programmatic access.

MethodProsConsUse Case
API KeysSimple, stateless, revocableStatic credentials, require careful managementServer-to-server integrations
OAuth 2.0User-delegated, scoped permissionsComplex flow, requires user interactionThird-party apps accessing user data
JWT TokensStateless, short-lived, signedRequires token refresh logicUser session authentication
Basic AuthExtremely simpleUsername/password in every requestLegacy systems only

Why API keys win for this platform:

  1. Server-to-server integrations — Most use cases are automated systems (CRMs, analytics tools)
  2. Simpler than OAuth — No user authorization flow needed for admin/broker operations
  3. Easier to manage — Single revocable token vs. managing user credentials
  4. Role-based access — Keys inherit permissions, no per-user configuration

1. Authentication

Is this a valid key?

Validates the key exists and hasn’t been revoked

2. Authorization

Does this key have permission?

Checks role-based access control (RBAC)

3. Rate Limiting

Is this usage pattern legitimate?

Prevents abuse and runaway scripts

  1. Random 256-bit value generated using CSPRNG
  2. Shown to user once in plaintext
  3. Hashed using bcrypt (work factor 12)
  4. Only hash stored in database

Result: Even database administrators cannot retrieve original keys.

  1. Client sends key in X-API-Key header
  2. Server hashes the provided key
  3. Compares hash to stored hash (constant-time comparison)
  4. Validates key is active (not revoked)
  5. Loads associated role and permissions

Result: Timing attacks cannot reveal valid keys.


Unauthorized Access

Only users with valid keys can access the API

Mitigation: Key validation, role-based permissions

Privilege Escalation

Agent keys cannot perform admin operations

Mitigation: Role-based access control (RBAC)

Denial of Service

Runaway scripts can’t exhaust server resources

Mitigation: Rate limiting per key

Data Exfiltration

Limits on how much data can be extracted

Mitigation: Rate limits, audit logging

ThreatWhy Keys Don’t HelpActual Mitigation
Key TheftStolen key works like the originalKey rotation, secure storage, monitoring
Insider ThreatsLegitimate users with malicious intentAudit logging, least privilege, separation of duties
Network SniffingKeys sent in HTTP headersHTTPS/TLS encryption (required)
Database BreachHashes can be cracked offlineStrong hashing (bcrypt), database encryption

  1. Admin Key Exposure

    • Impact: Full system access, data deletion, user creation
    • Likelihood: Medium (human error, git commits, logs)
    • Mitigation: Strict admin key policies, regular rotation, monitoring
  2. Credential Stuffing

    • Impact: Compromised keys from other breaches
    • Likelihood: Low (unique random keys, not reused passwords)
    • Mitigation: N/A (API keys not password-based)
  3. Rate Limit Bypass

    • Impact: Resource exhaustion, data scraping
    • Likelihood: Low (enforced at application level)
    • Mitigation: Per-key and per-IP rate limiting
  1. Broker Key Misuse

    • Impact: Access to competitors’ brokerage data
    • Likelihood: Low (keys scoped to brokerage)
    • Mitigation: Brokerage isolation in database queries
  2. Logging Sensitive Data

    • Impact: Keys exposed in application logs
    • Likelihood: Medium (developer error)
    • Mitigation: Automated log redaction, code review
  3. Revoked Key Caching

    • Impact: Brief window where revoked keys still work
    • Likelihood: Low (immediate database check)
    • Mitigation: No caching of key validation

Security Best Practices (Admin Perspective)

Section titled “Security Best Practices (Admin Perspective)”

Creation:

  • Require justification for admin-level keys
  • Set expiration dates for temporary integrations
  • Document key purpose in metadata

Usage:

  • Monitor request patterns for anomalies
  • Alert on unusual error rates (>10%)
  • Review last-used timestamps monthly

Rotation:

  • Force rotation every 90 days for critical systems
  • Provide advance notice to integration owners
  • Validate new keys before revoking old ones

Revocation:

  • Immediate revocation on suspected compromise
  • Grace period (24h) for planned migrations
  • Audit log entry with reason for revocation

Set up alerts for:

Alert TypeThresholdAction
High Error Rate>10% 4xx/5xx responsesInvestigate integration issue
Rate Limit HitKey hits limit 3+ times/dayContact integration owner
Geographic AnomalyRequests from unexpected countryPotential key theft
After-Hours UsageActivity outside business hoursReview for automation gone wrong
Dormant Key ActivityUnused key suddenly activePotential compromise

Similarities:

  • Prefixed format (sk_, pk_ vs. idx_)
  • Role-based (secret vs. publishable)
  • Revocable at any time

Differences:

  • Stripe has restricted keys (scoped permissions)
  • HomeStar uses role-based, not scope-based

Similarities:

  • Paired with secret (Access Key ID + Secret)
  • Role-based permissions (IAM policies)

Differences:

  • HomeStar uses single-token authentication
  • No separate “secret” component
  • Simpler rotation process

Similarities:

  • Fine-grained permissions (scopes)
  • Expiration dates

Differences:

  • HomeStar uses role-based, not scope-based
  • Designed for server-to-server, not user delegation

OAuth 2.0 is the industry standard for user-delegated API access, but it’s overkill for this platform.

sequenceDiagram
User->>App: Clicks "Connect to CRM"
App->>OAuth Server: Authorization request
OAuth Server->>User: Login & grant permissions
User->>OAuth Server: Approves
OAuth Server->>App: Authorization code
App->>OAuth Server: Exchange code for token
OAuth Server->>App: Access token
App->>API: Request with token
sequenceDiagram
Admin->>Dashboard: Create API key
Dashboard->>Admin: Returns key
Admin->>Integration: Configures key
Integration->>API: Request with key

When OAuth would be better:

  • Third-party apps accessing user data
  • Granular permissions per integration
  • User-initiated access revocation

Why API keys suffice:

  • Admin-controlled integrations (no user delegation)
  • Role-based permissions are sufficient
  • Simpler for server-to-server automation

Potential improvements to the key system:

  1. IP Whitelisting — Restrict keys to specific IP ranges
  2. Scoped Permissions — Agent keys with read-only property access
  3. Key Expiration — Enforce automatic expiration dates
  4. Multi-Factor Authentication — Require 2FA for admin key creation
  5. Anomaly Detection — Machine learning to identify suspicious patterns
  6. Mutual TLS — Certificate-based authentication for high-security integrations