Skip to main content

Instagram Integration API

CivicPulse integrates with the Instagram Graph API to fetch recent @mentions of the configured Instagram Business account and convert them into trackable civic issues. The integration is surfaced in the Social Wall Instagram tab.

Demo vs Live Mode​

ConditionModeSource
IG_ACCESS_TOKEN or IG_USER_ID not setdemoReturns MOCK_IG_MENTIONS (5 sample civic posts)
Token set, API returns 401/403demoGraceful fallback to mock data
Token set, API returns dataliveReal mentions from Instagram Graph API

Environment Variables​

VariableRequiredDefaultDescription
IG_ACCESS_TOKENNoβ€”Long-lived user access token from Meta Developer portal. Without this, the Social Wall Instagram tab runs in demo mode.
IG_USER_IDNoβ€”Instagram Business account user ID. Required alongside IG_ACCESS_TOKEN for live mode.
IG_HANDLENocivicpulseDisplay name for the Instagram handle (without @). Used in API responses and the frontend.

Endpoints​

GET /api/ig/mentions​

Fetch recent mentions of the configured Instagram handle.

Auth: None required

Query params:

ParamTypeDefaultDescription
countnumber20Max mentions to return (clamped 10–50)

Response:

{
"mentions": [
{
"id": "ig1",
"text": "@civicpulse Massive pothole on Western Express Highway...",
"user": "@mumbai_civic_watch",
"timestamp": "5 min ago",
"category_hint": "road",
"city_hint": "Mumbai",
"images": [
{ "id": "ig1-img-0", "url": "https://...", "type": "image", "source": "instagram", "caption": "Pothole near flyover" }
],
"videos": [],
"permalink": "https://www.instagram.com/p/abc123/"
}
],
"mode": "demo",
"handle": "civicpulse"
}

Headers: Cache-Control: no-store


POST /api/ig/mentions/:mediaId/convert​

Convert an Instagram mention into a CivicPulse issue.

Auth: Bearer token required

Path params:

ParamTypeDescription
mediaIdstring (1–50)Instagram media ID to associate with the issue

Body:

FieldTypeRequiredDescription
titlestring (5–200)YesIssue title
descriptionstring (10–5000)YesIssue description
categorystringYesOne of: traffic, road, infrastructure, hygiene, healthcare, systemic
citystring (2–100)YesCity name
prioritystringNolow, medium (default), high
imagesarrayNoImage media items to attach
videosarrayNoVideo media items to attach

Response: 201 β€” created issue object with source: "instagram" and tweetId set to the media ID.


Instagram Graph API Setup​

Prerequisites​

  1. Instagram Business or Creator account (free to convert from personal)
  2. Facebook Page linked to the Instagram account
  3. Meta Developer App with instagram_basic and instagram_manage_comments permissions

Getting Credentials​

  1. Go to Meta Developer Portal
  2. Create or select your app
  3. Add the Instagram Graph API product
  4. Generate a User Access Token with required permissions
  5. Convert to a long-lived token (60-day expiry, refreshable)
  6. Find your Instagram User ID via GET /me?fields=id,username

Token Refresh​

Long-lived tokens expire after 60 days. Refresh before expiry:

GET https://graph.facebook.com/v18.0/oauth/access_token
?grant_type=ig_exchange_token
&client_secret={app-secret}
&access_token={long-lived-token}

Mention Shape​

Each mention (both mock and live) follows this shape:

interface IGMention {
id: string; // Instagram media ID
text: string; // Caption text
user: string; // "@username"
timestamp: string; // Relative time ("5 min ago")
category_hint: string; // Auto-detected from text
city_hint: string; // Auto-detected from text
images: MediaItem[]; // IMAGE or CAROUSEL_ALBUM β†’ images
videos: MediaItem[]; // VIDEO β†’ videos
permalink: string; // "https://www.instagram.com/p/..."
}

Category Detection​

The same detectCategory() function from textDetection.js is used across all social integrations (X, WhatsApp, Instagram). It scans the caption text for civic keywords and returns the best-matching category.

City Detection​

The same detectCity() function scans for known Indian city names in the caption text.


Backend Service​

File: backend/services/igService.js

Mirrors the structure of xService.js:

ExportDescription
fetchMentions(count)Main function β€” returns { mentions, mode }
formatIGMedia(media)Maps Instagram Graph API media object to CivicPulse shape
IG_HANDLEConfigured Instagram handle
MOCK_IG_MENTIONS5 mock mentions for demo mode

Frontend Components​

IGFeed​

File: frontend/src/components/social/IGFeed.jsx

Two-column layout mirroring WAFeed:

  • Left column: Pending IG posts with username, caption, media thumbnails, city/category badges, "Convert" button
  • Right column: Already-converted issues filtered by source === 'instagram'

Instagram-branded styling: gradient purple-pink (#833AB4 β†’ #E1306C).

SocialWall Integration​

The SocialWall component renders IGFeed when the Instagram tab is selected, replacing the previous "Coming Soon" placeholder. A mode badge shows "Demo" or "Live Instagram" status.