37 - API Key & Playground

Overview

OpenAlgo provides a secure API key management system and an interactive API Playground for testing REST API and WebSocket endpoints. API keys are hashed using Argon2 with pepper for storage and encrypted using Fernet for retrieval.

Architecture Diagram

┌──────────────────────────────────────────────────────────────────────────────┐
│                        API Key Architecture                                   │
└──────────────────────────────────────────────────────────────────────────────┘

                      Generate API Key Request


┌─────────────────────────────────────────────────────────────────────────────┐
│                         API Key Generation                                   │
│                                                                              │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │  api_key = secrets.token_hex(32)  # 64 character hex string         │   │
│  │                                                                      │   │
│  │  Example: a1b2c3d4e5f6...789012345678901234567890abcdef12345678     │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
└──────────────────────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────────────────┐
│                         Dual Storage Strategy                                │
│                                                                              │
│  ┌──────────────────────────────┐  ┌──────────────────────────────────────┐│
│  │   Hashed (Argon2 + Pepper)   │  │  Encrypted (Fernet)                  ││
│  │   For API authentication     │  │  For TradingView integration         ││
│  │                              │  │                                       ││
│  │  hash = argon2.hash(        │  │  encrypted = fernet.encrypt(         ││
│  │    api_key + pepper         │  │    api_key                            ││
│  │  )                          │  │  )                                    ││
│  │                              │  │                                       ││
│  │  → Stored in api_key_hash   │  │  → Stored in encrypted_api_key        ││
│  └──────────────────────────────┘  └──────────────────────────────────────┘│
└─────────────────────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────────────────┐
│                         api_keys Table (SQLite)                              │
│                                                                              │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │  id | user_id | api_key_hash | encrypted_api_key | order_mode      │   │
│  │  ───┼─────────┼──────────────┼───────────────────┼─────────────────│   │
│  │  1  | admin   | $argon2id... | gAAAAA...         | auto            │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────────────────────┘

API Key Generation

Location: blueprints/apikey.py

Key Properties

Property
Value

Length

64 characters (hex)

Entropy

256 bits

Format

Hexadecimal (0-9, a-f)

Generation

secrets.token_hex(32)

API Key Storage

Dual Storage for Different Use Cases

Three-Level Verification

Order Mode

Auto vs Semi-Auto Mode

Mode
Description
Use Case

auto

Orders execute immediately

Personal trading

semi_auto

Orders require manual approval

Managed accounts

API Playground

Location: blueprints/playground.py

Architecture

Endpoint Categories

API Endpoints

Endpoint
Method
Description

/playground/

GET

Render playground UI

/playground/api-key

GET

Get user's API key

/playground/collections

GET

Get Postman/Bruno collections

/playground/endpoints

GET

Get structured endpoint list

WebSocket Testing

WebSocket Endpoint Format in Bruno

WebSocket Actions

Action
Description

subscribe

Subscribe to symbols

unsubscribe

Unsubscribe from symbols

API Usage Examples

Using API Key in Requests

TradingView Integration

Security Considerations

API Key Protection

Layer
Protection

Storage

Argon2 hash + Fernet encryption

Transit

HTTPS recommended

Verification

Pepper + constant-time comparison

Caching

TTLCache (expires after broker logout)

Playground Security

  • Session authentication required

  • CSRF protection (exempted for API endpoints)

  • API key auto-populated from session

  • No API key logging

Key Files Reference

File
Purpose

blueprints/apikey.py

API key CRUD operations

blueprints/playground.py

API testing playground

database/auth_db.py

API key storage/verification

collections/**/*.bru

Bruno endpoint definitions

templates/playground.html

Playground UI template

frontend/src/pages/ApiKey.tsx

React API key page

Last updated