Here is the complete Technical Architecture Documentation for your Instagram Automation System (v2026).
Save this. It explains exactly how your system works, why it works, and how every piece connects. 🏛️
This system is built on the "Instagram API with Instagram Login" standard (the new 2025/2026 protocol). It does not use Facebook Login. It relies on a Self-Healing Identity System that automatically resolves the difference between "Login IDs" (IGSID) and "Business IDs" without user intervention.
File: components/layout/landing-page.tsx
This is the entry point. We moved away from the old api.instagram.com auth to the new www.instagram.com business flow.
-
Host:
https://www.instagram.com/oauth/authorize(Forces the Business Login UI). -
Scopes:
-
instagram_business_basic: Essential for reading profile info. -
instagram_business_manage_messages: Required for DMs. -
instagram_business_manage_comments: Required for Comments. -
instagram_business_content_publish: Required for some auto-responses. -
Key Parameter:
force_reauth=true. This ensures the user actually sees the login screen, preventing accidental connections to the wrong cached account.
File: app/api/instagram/callback/route.ts
This handles the exchange of the temporary Code for a permanent Token. It performs Identity Discovery.
- Exchange Code: Swaps the code for a Short-Lived Token using
api.instagram.com. - Upgrade Token: Swaps the Short Token for a Long-Lived Token (60 Days) using
graph.instagram.com. - Discovery (The "Gold" Check):
- The code asks Instagram: "Who is the Business Account for this user?"
- If Successful: It gets the Real Business ID (starts with
1784...). - If Failed: It returns
null(we handle this later).
- The "Safety" Save:
- It saves the Login ID (starts with
256...) as a fallback. - It saves the Business ID (starts with
1784...) if found. - Crucial Logic: It checks if the DB already has a valid
1784...ID. If yes, it protects it and refuses to overwrite it with a256...ID.
File: app/api/instagram/webhook/route.ts
This is the event listener. It receives messages from Instagram. It has three layers of intelligence.
Before doing anything, the bot checks: "Is this message from ME?"
- It checks for
is_echo: true,delivery, orreadreceipts. - Why? If the bot replies, Instagram sends an event back. If we don't silence this, the bot tries to find a user for its own ID, fails, and throws "Token Mismatch" errors.
- Action: If it's noise, the bot Stops Immediately.
When a real message comes in (e.g., ID 1784...963):
- The bot searches the database:
SELECT * FROM users WHERE business_account_id = '...' OR page_id = '...' - Why? Sometimes Instagram uses the "Main ID", sometimes the "Shadow ID". By checking both columns, we always find the user.
If the ID is NOT in the database (e.g., Discovery failed during login):
- Find Candidate: It grabs the most recently logged-in user.
- Test Token: It uses that user's Access Token to try and fetch the Webhook ID.
- The Decision:
- If the API says "OK": It proves this user owns this ID.
- Action: It Automatically Updates the Database, saving the ID into the
business_account_idcolumn.
- Result: The bot replies instantly, and the user is fixed forever.
Table: users
We utilize a flexible schema to handle the "Dual Identity" of Instagram accounts.
id(Text, PK): The Login ID (IGSID, starts with256...). Used for authentication.username(Text): Visual reference only.access_token(Text): The key to the API.business_account_id(Text): The Main Business ID (starts with1784...). Used for sending messages and receiving Echos.page_id(Text): The Shadow ID or Fallback ID. Used for receiving some webhooks or as a backup.
We strictly adhere to the 2026 Standard:
- Authentication:
api.instagram.com - Data & Actions:
graph.instagram.com(NOTgraph.facebook.com). - Version:
v24.0(The latest stable release).
- We don't trust IDs: We know IDs can change format (
256vs1784), so we save both. - We assume failure: We assume "Discovery" might fail, so we built "Self-Healing" to fix it later.
- We ignore ourselves: We filter out our own "Echos" so the logs stay clean.
You now have a system that is Robust, Self-Correcting, and Scale-Ready. 🚀