AI News Hub Logo

AI News Hub

How to pull TikTok Ads reports straight into Google Sheets

DEV Community
Julian Reiter

The TikTok Ads Manager UI is fine for browsing, but if you run more before you've made coffee. The platform's CSV export button This post walks through replacing that chore with a 30-line cron job, aigen-reports. Every morning at 02:00 your server pulls yesterday's campaign metrics Mon 03:00 → 12 rows (4 campaigns × 3 metrics) → Sheets:Daily!A2 Tue 03:00 → 12 rows → Sheets:Daily!A14 Wed 03:00 → 12 rows → Sheets:Daily!A26 Python 3.10+ on the machine that will run cron. A TikTok for Business account with at least one advertiser (sandbox works for testing — you can switch to a real account once approved). A Google Cloud project with the Sheets API enabled and a service account JSON key file. (Optional — if you only want CSV output you can skip this.) Open https://business-api.tiktok.com/portal/. Click Become a developer → fill in the basic profile. Click Create an App. App name: anything (this is what reviewers see — make it specific). App description: 2–3 sentences saying it's a read-only reporting utility. Don't oversell. Permission category: tick Reporting only. No write categories, no Ads Management, no Creative Management. The fewer categories you ask for the faster the review. Set the redirect URI to http://localhost:8765/callback for local testing. Submit for review. While you wait, you can develop against the sandbox. pip install aigen-reports Set your app credentials: export TIKTOK_APP_ID=... export TIKTOK_APP_SECRET=... export TIKTOK_REDIRECT_URI=http://localhost:8765/callback Run the auth flow: aigen-reports auth The browser opens, you click Authorize, the CLI exchanges the auth ~/.aigen-reports/credentials.json. Token lifecycle. TikTok access tokens expire (typically 24 h); aigen-reports pull \ --advertiser 7xxxxxxxxxx \ --metric spend,clicks,impressions,conversions \ --breakdown campaign \ --range last-1d \ --out yesterday.csv That's the entire reporting flow. The output is a plain CSV: campaign_id,campaign_name,stat_time_day,spend,clicks,impressions,conversions 17xxxxxxxxx,Evergreen-EN,2026-04-16,148.21,512,18342,9 17xxxxxxxxx,Evergreen-DE,2026-04-16,212.04,743,24011,14 ... If you want the output in Sheets instead of CSV, install the optional --out at a sheet ID: pip install 'aigen-reports[gsheet]' export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json aigen-reports pull \ --advertiser 7xxxxxxxxxx \ --metric spend,clicks,conversions \ --range last-1d \ --out gsheet:1AbCdEfGhIjKlMnOpQrStUvWxYz Share the Sheet with the service account's email so it can append. Add the same command to cron: # /etc/cron.d/aigen-reports 0 2 * * * www-data /usr/local/bin/aigen-reports pull \ --advertiser 7xxxxxxxxxx \ --metric spend,clicks,conversions \ --range last-1d \ --out gsheet:1AbCdEfGhIjKlMnOpQrStUvWxYz Done. From here on you don't open the TikTok Ads Manager UI just to code: 40105 access_token has expired — the refresh failed, run aigen-reports auth again. code: 40002 advertiser_id not in your business — the operator who authorized the app no longer has access to that advertiser. Re- authorize with someone who does. Rate-limit response (code: 50002) — TikTok caps requests per app per minute. The CLI retries with exponential backoff; if you're pulling 50+ accounts in parallel, stagger them across the hour. It is not a dashboard. It writes a CSV, that's it. It is not a campaign manager. No write scopes are requested. It is not a hosted SaaS. It runs on your machine, your data stays on your machine. If you need a dashboard, paid hosting, or campaign management, this CLI: https://github.com/ArtificialIntelligentGeneration/aigen-reports Curated docs / SDK list: https://github.com/ArtificialIntelligentGeneration/awesome-tiktok-ads-api Site: https://aigen-agency.com Bug reports on GitHub Issues, everything else by email. — Julian