# sexlovr/grok-2-api architecture

> Architecture diagram of the GitHub repository sexlovr/grok-2-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sexlovr/grok-2-api
- Repository: https://github.com/sexlovr/grok-2-api

## Overview

Grok2API is an OpenAI-compatible proxy whose main workflow converts chat messages to Grok’s single-prompt format, chooses a model mode, and routes the request through a browser-tab phone relay when available or a direct Grok client otherwise. The admin workflow manages accounts and API keys and installs a userscript that refreshes signatures and provides phone egress. The excerpts show the main modules and several route calls; some relay and dashboard wiring is documented rather than fully excerpted.

Main entry points: [`index.js`](https://github.com/sexlovr/grok-2-api/blob/main/index.js) (HTTP API server), [`lib/grokClient.js`](https://github.com/sexlovr/grok-2-api/blob/main/lib/grokClient.js) (Direct Grok client), [`lib/page.js`](https://github.com/sexlovr/grok-2-api/blob/main/lib/page.js) (Admin dashboard), [`lib/refresher.js`](https://github.com/sexlovr/grok-2-api/blob/main/lib/refresher.js) (Signature refresh)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API workflow"]
  node_server["HTTP API server<br/>[index.js]"]
  node_mode["Model selection<br/>[index.js]"]
end

subgraph group_egress["Grok egress"]
  node_relay_hub["Relay job hub<br/>[relay.js]"]
  node_phone_worker["Browser-tab worker<br/>[refresher.js]"]
  node_sig_refresh["Signature refresh<br/>[refresher.js]"]
  node_grok_client["Direct Grok client<br/>[grokClient.js]"]
end

subgraph group_admin["Admin and accounts"]
  node_admin_ui["Admin dashboard<br/>[page.js]"]
  node_account_import["cURL account parsing<br/>[curlParser.js]"]
  node_admin_routes["Account and key management<br/>[index.js]"]
end

subgraph group_state["State and translation"]
  node_prompt["Prompt conversion<br/>[translator.js]"]
  node_stream_parse["NDJSON parsing<br/>[grokClient.js]"]
  node_response_format["OpenAI response formatting<br/>[translator.js]"]
  node_database[("SQLite state<br/>[database.js]")]
end

node_api_client(("OpenAI client"))
node_grok_service{{"Grok web API"}}
node_admin_user(("Admin user"))

node_api_client -->|"sends requests"| node_server
node_server -->|"converts messages"| node_prompt
node_server -->|"resolves model"| node_mode
node_server -->|"queues jobs"| node_relay_hub
node_phone_worker -->|"polls and returns chunks"| node_relay_hub
node_phone_worker -->|"sends signed chat"| node_grok_service
node_sig_refresh -->|"refreshes signature"| node_grok_service
node_sig_refresh -->|"posts signature"| node_server
node_server -->|"uses direct fallback"| node_grok_client
node_grok_client -->|"posts chat request"| node_grok_service
node_grok_client -->|"feeds response lines"| node_stream_parse
node_server -->|"parses relay stream"| node_stream_parse
node_server -->|"formats API output"| node_response_format
node_response_format -->|"returns completion"| node_api_client
node_admin_user -->|"uses dashboard"| node_admin_ui
node_admin_ui -->|"calls admin endpoints"| node_admin_routes
node_admin_routes -->|"parses account cURL"| node_account_import
node_admin_routes -->|"reads and writes state"| node_database
node_server -->|"loads model and account state"| node_database
node_admin_ui -->|"offers userscript install"| node_sig_refresh

click node_server "https://github.com/sexlovr/grok-2-api/blob/main/index.js"
click node_prompt "https://github.com/sexlovr/grok-2-api/blob/main/lib/translator.js"
click node_mode "https://github.com/sexlovr/grok-2-api/blob/main/index.js"
click node_relay_hub "https://github.com/sexlovr/grok-2-api/blob/main/lib/relay.js"
click node_phone_worker "https://github.com/sexlovr/grok-2-api/blob/main/lib/refresher.js"
click node_sig_refresh "https://github.com/sexlovr/grok-2-api/blob/main/lib/refresher.js"
click node_grok_client "https://github.com/sexlovr/grok-2-api/blob/main/lib/grokClient.js"
click node_stream_parse "https://github.com/sexlovr/grok-2-api/blob/main/lib/grokClient.js"
click node_response_format "https://github.com/sexlovr/grok-2-api/blob/main/lib/translator.js"
click node_admin_ui "https://github.com/sexlovr/grok-2-api/blob/main/lib/page.js"
click node_account_import "https://github.com/sexlovr/grok-2-api/blob/main/lib/curlParser.js"
click node_admin_routes "https://github.com/sexlovr/grok-2-api/blob/main/index.js"
click node_database "https://github.com/sexlovr/grok-2-api/blob/main/lib/database.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_server,node_mode,node_api_client,node_admin_user toneBlue
class node_relay_hub,node_phone_worker,node_sig_refresh,node_grok_client toneAmber
class node_admin_ui,node_account_import,node_admin_routes,node_grok_service toneMint
class node_prompt,node_stream_parse,node_response_format,node_database toneRose
```

## Components

### API workflow

- [HTTP API server](https://github.com/sexlovr/grok-2-api/blob/main/index.js): `index.js`
- [Model selection](https://github.com/sexlovr/grok-2-api/blob/main/index.js): `index.js`

### Grok egress

- [Relay job hub](https://github.com/sexlovr/grok-2-api/blob/main/lib/relay.js): `lib/relay.js`
- [Browser-tab worker](https://github.com/sexlovr/grok-2-api/blob/main/lib/refresher.js): `lib/refresher.js`
- [Signature refresh](https://github.com/sexlovr/grok-2-api/blob/main/lib/refresher.js): `lib/refresher.js`
- [Direct Grok client](https://github.com/sexlovr/grok-2-api/blob/main/lib/grokClient.js): `lib/grokClient.js`

### Admin and accounts

- [Admin dashboard](https://github.com/sexlovr/grok-2-api/blob/main/lib/page.js): `lib/page.js`
- [cURL account parsing](https://github.com/sexlovr/grok-2-api/blob/main/lib/curlParser.js): `lib/curlParser.js`
- [Account and key management](https://github.com/sexlovr/grok-2-api/blob/main/index.js): `index.js`

### State and translation

- [Prompt conversion](https://github.com/sexlovr/grok-2-api/blob/main/lib/translator.js): `lib/translator.js`
- [NDJSON parsing](https://github.com/sexlovr/grok-2-api/blob/main/lib/grokClient.js): `lib/grokClient.js`
- [OpenAI response formatting](https://github.com/sexlovr/grok-2-api/blob/main/lib/translator.js): `lib/translator.js`
- [SQLite state](https://github.com/sexlovr/grok-2-api/blob/main/lib/database.js): `lib/database.js`

### Other components

- **OpenAI client**
- **Grok web API**
- **Admin user**

## Connections

- OpenAI client → HTTP API server: sends requests (evidence: `README.md`)
- HTTP API server → Prompt conversion: converts messages (evidence: `index.js`)
- HTTP API server → Model selection: resolves model (evidence: `index.js`)
- HTTP API server → Relay job hub: queues jobs (evidence: `README.md`)
- Browser-tab worker → Relay job hub: polls and returns chunks (evidence: `README.md`)
- Browser-tab worker → Grok web API: sends signed chat (evidence: `README.md`)
- Signature refresh → Grok web API: refreshes signature (evidence: `lib/refresher.js`)
- Signature refresh → HTTP API server: posts signature (evidence: `lib/refresher.js`)
- HTTP API server → Direct Grok client: uses direct fallback (evidence: `README.md`)
- Direct Grok client → Grok web API: posts chat request (evidence: `lib/grokClient.js`)
- Direct Grok client → NDJSON parsing: feeds response lines (evidence: `lib/grokClient.js`)
- HTTP API server → NDJSON parsing: parses relay stream (evidence: `index.js`)
- HTTP API server → OpenAI response formatting: formats API output (evidence: `index.js`)
- OpenAI response formatting → OpenAI client: returns completion (evidence: `README.md`)
- Admin user → Admin dashboard: uses dashboard (evidence: `README.md`)
- Admin dashboard → Account and key management: calls admin endpoints (evidence: `lib/page.js`)
- Account and key management → cURL account parsing: parses account cURL (evidence: `index.js`)
- Account and key management → SQLite state: reads and writes state (evidence: `index.js`)
- HTTP API server → SQLite state: loads model and account state (evidence: `index.js`)
- Admin dashboard → Signature refresh: offers userscript install (evidence: `README.md`)
