2026-05-07 14:46:36 +01:00
# Ollama Cloud Usage Server
2026-05-25 11:23:31 +01:00
Lightweight Node.js server that pulls your Ollama cloud usage from `ollama.com/settings` and serves it as a JSON API + web dashboard. Runs fine on headless servers. Can optionally push metrics to Home Assistant.
2026-05-07 14:46:36 +01:00
2026-05-08 09:04:45 +01:00
**No browser required on the server** — just HTTP requests with cookie management.
2026-05-07 14:46:36 +01:00
## Setup
```bash
cp .env.example .env # Edit with your settings
npm install
npm start
```
2026-05-25 11:23:31 +01:00
## Web Dashboard
Open `http://localhost:3214` in your browser for a built-in dashboard that shows:
- Current session and weekly usage with progress bars
- Per-model request breakdowns
- A form to paste/update cookies without curl
- Server health info (auth mode, poll interval, last fetch)
2026-05-07 14:46:36 +01:00
## Authentication
2026-05-08 09:04:45 +01:00
Pick one:
2026-05-07 14:46:36 +01:00
2026-05-08 09:04:45 +01:00
### Option 1: Email/Password (if your account has a password)
2026-05-07 14:46:36 +01:00
2026-05-08 09:04:45 +01:00
Add to `.env` :
2026-05-07 14:46:36 +01:00
```
AUTH_MODE=email
OLLAMA_EMAIL=you@example .com
OLLAMA_PASSWORD=your-password
```
2026-05-25 11:23:31 +01:00
The server will log in automatically and manage session cookies. If you signed up with Google OAuth and never set a password, skip to Option 2.
2026-05-07 14:46:36 +01:00
2026-05-08 09:04:45 +01:00
### Option 2: Export cookies from your browser (for Google/GitHub sign-ins)
2026-05-07 14:46:36 +01:00
2026-05-08 09:04:45 +01:00
**Using a cookie extension** like [EditThisCookie ](https://editthiscookie.com/ ) or [Cookie-Editor ](https://cookie-editor.com/ ):
1. Install the extension, log into ollama.com
2026-05-07 14:46:36 +01:00
2. Export cookies for ollama.com as JSON
3. POST the JSON array to the server:
```bash
curl -X POST http://localhost:3214/api/cookies \
-H "Content-Type: application/json" \
-d @cookies .json
```
2026-05-25 11:23:31 +01:00
Or paste them directly in the web dashboard at `http://localhost:3214` .
2026-05-08 09:04:45 +01:00
**Or grab them manually from DevTools:**
1. Log into ollama.com in your browser
2026-05-07 14:46:36 +01:00
2. Open DevTools → Application → Cookies → `https://ollama.com`
3. Copy each cookie's name/value and POST as JSON
2026-05-25 11:23:31 +01:00
Cookies get saved to `.cookies.json` (next to `server.js` ) and reused automatically. They'll expire eventually — when that happens, just re-export and POST them again.
2026-05-07 14:46:36 +01:00
**Set in `.env` :**
```
AUTH_MODE=cookies
```
## API Endpoints
2026-05-08 09:04:45 +01:00
| Endpoint | Method | What it does |
2026-05-07 14:46:36 +01:00
|---|---|---|
2026-05-25 11:23:31 +01:00
| `/` | GET | Web dashboard |
2026-05-08 09:04:45 +01:00
| `/api/usage` | GET | Latest usage data from last poll |
2026-05-07 14:46:36 +01:00
| `/api/usage/refresh` | GET | Force a fresh poll right now |
| `/api/health` | GET | Server status and config |
2026-05-08 09:04:45 +01:00
| `/api/cookies` | POST | Upload new cookies (JSON array) |
2026-05-07 14:46:36 +01:00
### Example response (`/api/usage`)
```json
{
"session": {
"percent": 9.2,
2026-05-25 11:23:31 +01:00
"resetsIn": "2 hours",
"models": [
{ "model": "gemma3", "requests": 14 },
{ "model": "llama3", "requests": 3 }
]
2026-05-07 14:46:36 +01:00
},
"weekly": {
"percent": 32.9,
2026-05-25 11:23:31 +01:00
"resetsIn": "3 days",
"models": [
{ "model": "gemma3", "requests": 142 },
{ "model": "llama3", "requests": 37 }
]
2026-05-07 14:46:36 +01:00
},
"fetchedAt": "2026-05-07T13:08:44.194Z"
}
```
## Home Assistant Integration
### Option A: Automatic push
2026-05-25 11:23:31 +01:00
Set `HA_URL` and `HA_TOKEN` in `.env` . After every poll, the server pushes to these sensors (HA creates them automatically):
| Sensor | Description |
|---|---|
| `sensor.ollama_session_usage` | Session usage % |
| `sensor.ollama_session_models` | Number of models used this session (attributes include per-model request counts) |
| `sensor.ollama_weekly_usage` | Weekly usage % |
| `sensor.ollama_weekly_models` | Number of models used this week (attributes include per-model request counts) |
2026-05-07 14:46:36 +01:00
2026-05-08 09:04:45 +01:00
### Option B: REST sensor (pull from HA side)
2026-05-07 14:46:36 +01:00
Add to `configuration.yaml` :
```yaml
sensor:
- platform: rest
name: Ollama Session Usage
resource: http://localhost:3214/api/usage
value_template: "{{ value_json.session.percent | default('unavailable') }}"
unit_of_measurement: "%"
state_class: measurement
json_attributes:
- session
- weekly
- fetchedAt
scan_interval: 1800
- platform: rest
name: Ollama Weekly Usage
resource: http://localhost:3214/api/usage
value_template: "{{ value_json.weekly.percent | default('unavailable') }}"
unit_of_measurement: "%"
state_class: measurement
scan_interval: 1800
```
## Environment Variables
| Variable | Default | Description |
|---|---|---|
| `AUTH_MODE` | `email` if password set, else `cookies` | `email` or `cookies` |
| `OLLAMA_EMAIL` | — | Email for email auth mode |
| `OLLAMA_PASSWORD` | — | Password for email auth mode |
| `PORT` | 3214 | Server port |
2026-05-08 09:04:45 +01:00
| `POLL_MINUTES` | 30 | How often to poll (minutes) |
2026-05-07 14:46:36 +01:00
| `HA_URL` | — | Home Assistant URL (no trailing slash) |
| `HA_TOKEN` | — | HA long-lived access token |
2026-05-25 11:23:31 +01:00
| `COOKIE_FILE` | `.cookies.json` | Where to store cookies (defaults next to server.js) |
2026-05-07 14:46:36 +01:00
## Updating Cookies
2026-05-08 09:04:45 +01:00
When cookies expire, just re-export from your browser and POST again — it replaces the old ones:
2026-05-07 14:46:36 +01:00
```bash
curl -X POST http://localhost:3214/api/cookies \
-H "Content-Type: application/json" \
-d @fresh -cookies.json
```
2026-05-25 11:23:31 +01:00
Or use the web dashboard at `http://localhost:3214` .
2026-05-08 09:04:45 +01:00
Server logs will show `needsLogin` when cookies have expired.
2026-05-07 14:46:36 +01:00
## Running as a Service
```bash
# Using pm2
npm install -g pm2
pm2 start server.js --name ollama-usage
pm2 save
pm2 startup
2026-05-25 11:23:31 +01:00
```