Skip to content

Commit 138544d

Browse files
committed
Added experimental script to automate fetching Tinybird tokens
1 parent 1585fe0 commit 138544d

5 files changed

Lines changed: 51 additions & 4 deletions

File tree

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ ENABLE_ACTIVITYPUB=false
2323
# Tinybird configuration
2424
TINYBIRD_API_URL=https://api.tinybird.co
2525
TINYBIRD_TRACKER_TOKEN=p.eyJxxxxx
26-
TINYBIRD_STATS_TOKEN=p.eyJxxxxx
26+
TINYBIRD_ADMIN_TOKEN=p.eyJxxxxx
27+
TINYBIRD_WORKSPACE_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
2728

2829
# Data locations
2930
# Location to store uploaded data

TINYBIRD.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ Steps:
77
1. Run `docker compose run --rm tinybird-login` to login to your Tinybird account
88
1. Run `docker compose --profile=analytics up tinybird-sync`. This will copy the Tinybird files from the Ghost container into a shared volume. The service should log "Tinybird files synced into shared volume.", then exit.
99
1. Run `docker compose --profile=analytics up tinybird-deploy` and wait for the service to exit successfully. This will create your Tinybird datasources, pipes and API endpoints. It may take a minute or two to complete the first time. You should see "Deployment #1 is live!" in your terminal before the service exits.
10-
1. Find your workspace's events API endpoint: `docker compose run --rm tinybird-login tb --cloud info`, copy the value of "api", and add it to your `.env` file as `TINYBIRD_API_URL`. You can also find this value in your Tinybird Workspace's UI.
10+
1. Find your workspace's events API endpoint and workspace ID: `docker compose run --rm tinybird-login tb --cloud info`, copy the value of "api", and add it to your `.env` file as `TINYBIRD_API_URL`, then copy the value of "workspace_id", and add it to your `.env` file as `TINYBIRD_WORKSPACE_ID`. You can also find these values in your Tinybird Workspace's UI.
1111
1. Using the UI link from the previous step, open your workspace and click on *Tokens* in the left hand menu
12-
1. Copy your Tinybird `stats_page` token and add it to your `.env` file as `TINYBIRD_STATS_TOKEN`
12+
1. Copy your Tinybird "Workspace admin token" and add it to your `.env` file as `TINYBIRD_ADMIN_TOKEN`
1313
1. Copy your Tinybird `tracker` token and add it to your `.env` file as `TINYBIRD_TRACKER_TOKEN`
14+
1. In the Tinybird UI, click on *Settings* at the bottom of the left hand menu
15+
1. Copy your Tinybird Workspace ID and add it to your `.env` file as `TINYBIRD_WORKSPACE_ID`
1416
1. Run `docker compose --profile=analytics up -d` to start all services in the background
1517
1. Set `COMPOSE_PROFILES=analytics` in your `.env` file to automatically include the `analytics` profile when running `docker compose` commands
1618
1. At this point, everything should be working. You can test it's working by visiting your site's homepage, then checking the Stats page in Ghost Admin — you should see a view recorded.

compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ services:
3939
labs__ActivityPub: ${ENABLE_ACTIVITYPUB:-false}
4040
tinybird__tracker__endpoint: https://${DOMAIN:?DOMAIN environment variable is required}/.ghost/analytics/tb/web_analytics
4141
tinybird__tracker__datasource: analytics_events
42+
tinybird__adminToken: ${TINYBIRD_ADMIN_TOKEN:-}
43+
tinybird__workspaceId: ${TINYBIRD_WORKSPACE_ID:-}
4244
tinybird__stats__endpoint: ${TINYBIRD_API_URL:-https://api.tinybird.co}
43-
tinybird__stats__token: ${TINYBIRD_STATS_TOKEN:-}
4445
volumes:
4546
- ${UPLOAD_LOCATION:-./data/ghost}:/var/lib/ghost/content
4647
depends_on:

tinybird/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ FROM python:3.13-slim
33
# Install dependencies
44
RUN apt-get update && apt-get install -y --no-install-recommends \
55
curl \
6+
jq \
67
ca-certificates \
78
&& rm -rf /var/lib/apt/lists/*
89

910
WORKDIR /home/tinybird
1011

1112
# Install Tinybird using the standard installation script
1213
COPY handleLogin.sh /usr/local/bin/tinybird-login
14+
COPY getTokens.sh /usr/local/bin/get-tokens
1315
COPY tb-wrapper /usr/local/bin/tb-wrapper
1416

1517
RUN curl https://tinybird.co | sh

tinybird/getTokens.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
# Get token from .tinyb JSON file
4+
USER_TOKEN=$(jq -r '.token' .tinyb)
5+
echo "User token: $USER_TOKEN"
6+
7+
if [ -z "$USER_TOKEN" ] || [ "$USER_TOKEN" = "null" ]; then
8+
echo "Error: Could not find token in .tinyb file"
9+
exit 1
10+
fi
11+
12+
# Get host from .tinyb JSON file
13+
HOST=$(jq -r '.host' .tinyb)
14+
echo "Host: $HOST"
15+
16+
if [ -z "$HOST" ] || [ "$HOST" = "null" ]; then
17+
echo "Error: Could not find host in .tinyb file"
18+
exit 1
19+
fi
20+
21+
# Get id from .tinyb JSON file
22+
WORKSPACE_ID=$(jq -r '.id' .tinyb)
23+
echo "Workspace ID: $WORKSPACE_ID"
24+
25+
if [ -z "$WORKSPACE_ID" ] || [ "$WORKSPACE_ID" = "null" ]; then
26+
echo "Error: Could not find id in .tinyb file"
27+
exit 1
28+
fi
29+
30+
# Make GET request to tokens endpoint and parse response
31+
RESPONSE=$(curl -s -X GET \
32+
"$HOST/v0/tokens" \
33+
-H "Authorization: Bearer $USER_TOKEN")
34+
35+
# Parse tokens from response
36+
ADMIN_TOKEN=$(echo "$RESPONSE" | jq -r '.tokens[] | select(.name == "admin token") | .token')
37+
TRACKER_TOKEN=$(echo "$RESPONSE" | jq -r '.tokens[] | select(.name == "tracker") | .token')
38+
39+
# Echo the tokens as proof of concept
40+
echo "Admin token: $ADMIN_TOKEN"
41+
echo "Tracker token: $TRACKER_TOKEN"

0 commit comments

Comments
 (0)