Instagram Analytics
Instagram Analytics: Backend & Token Operations
This page covers the Instagram service implementation, API contract, and token lifecycle.
Service Endpoints
| Method | Path | Auth | Purpose |
|---|---|---|---|
GET | /instagram/analytics | 🔓 | Return computed analytics payload |
POST | /instagram/token/refresh | 🔓 | Manual long-lived token refresh |
GET | /instagram/token/status | 🔓 | Token source + expiry status |
schedule | rate(1 day) | Internal | Automatic refresh check |
/instagram/analytics supports optional query params:
since(YYYY-MM-DD)until(YYYY-MM-DD)
Environment Variables
| Variable | Default | Description |
|---|---|---|
IG_ACCESS_TOKEN | "" | Primary fallback token source |
IG_DEFAULT_START_DATE | 2025-08-01 | Default since when omitted |
IG_REFRESH_LEAD_DAYS | 10 | Days-before-expiry threshold for scheduled refresh |
INSTAGRAM_GRAPH_BASE | https://graph.instagram.com/v25.0 | Graph API base override |
Note:
INSTAGRAM_GRAPH_BASEis not configured inserverless.ymlby default — the handler falls back tohttps://graph.instagram.com/v25.0if this variable is not set.
Token Source Precedence
When handling requests, the service resolves token in this order:
biztechInstagramAuth{ENV}table (id = primary)- environment token (
IG_ACCESS_TOKEN/INSTAGRAM_ACCESS_TOKEN) - missing token ->
500with clear message
Auto-Refresh Logic
Scheduled function runs daily and:
- checks current token source + stored expiry
- skips refresh if token is not near expiry
- refreshes with
refresh_access_tokenwhen due - stores updated token +
expiresAt+refreshedAtin DynamoDB - clears in-memory analytics cache
Manual refresh endpoint follows the same refresh+store path.
Analytics Payload (High Level)
GET /instagram/analytics returns:
- account metadata
- selected
since/until totalsobject (posts, likes, views, rates, averages)- account insight series (
reach,follower_count) monthlyrollupsmediaTypeBreakdown,weekdayBreakdown,hourBreakdowntopPostsgrouped by ranking metric- full
postslist with raw + derived metrics
In-memory cache:
- TTL: 5 minutes
- keyed by
since:until
DynamoDB Table
Table: biztechInstagramAuth{ENVIRONMENT}
Stored token record fields:
id(primary)accessTokenexpiresInexpiresAtrefreshedAtsource