Skip to content

feat(instagram): support Meta Unified Login - #1717

Open
gitpascal wants to merge 8 commits into
ChatbotXIO:mainfrom
gitpascal:feat/instagram-unified-login
Open

gitpascal wants to merge 8 commits into
ChatbotXIO:mainfrom
gitpascal:feat/instagram-unified-login

Conversation

@gitpascal

@gitpascal gitpascal commented Nov 13, 2026 •

Copy link
Copy Markdown

Closes #1716.

Summary

Adds support for Meta's Instagram API with Unified Login to the existing instagram-direct integration while preserving the current direct Instagram OAuth flow.

The flow is selected per platform credential:

  • with unifiedLoginConfigId: use Meta Unified Login;
  • without it: retain the existing instagram.com/oauth/authorize behavior.

What changed

  • add an optional Unified Login Configuration ID to Instagram platform credentials;
  • generate the Unified Login URL with client_id, config_id, response_type=code, and override_default_response_type=true;
  • exchange the code through the Graph API and resolve client_business_id;
  • paginate assigned_instagram_accounts and expose all returned accounts in the existing account-selection flow;
  • auto-complete API/MCP sessions whenever exactly one target is selectable, preserving the legacy one-account behavior;
  • store the Unified business access token and selected Instagram professional account ID;
  • route messaging, profile, attachment, comment, media, sync, and webhook calls through the correct Graph host/account ID;
  • subscribe Unified-compatible webhook fields;
  • keep legacy refresh-token behavior only for direct Instagram login;
  • document the required non-expiring Business Integration System User token and the CLI/MCP/setup flow;
  • add no database migration: the credential and auth metadata fields are optional.

Compatibility

The existing direct Instagram login remains the default when no Unified Login Configuration ID is set. Existing credentials and connected channels keep their previous code path.

Validation

  • Instagram integration: 32 files / 144 tests passed on the current main merge
  • Database credential tests: 19 passed
  • Builder connection/API tests: 3 files / 109 tests passed
  • CLI collision/smoke suite: 5 files / 60 tests passed
  • Instagram and Builder TypeScript checks
  • Translation and Ultracite checks
  • Unified OAuth URL, token exchange, account pagination/selection, Graph routing, webhook fields, and fallback behavior covered by tests
  • Live Meta OAuth smoke test against a production-mode Unified Login app

Live end-to-end validation completed successfully on a self-hosted installation:

  • Meta Unified Login OAuth and account selection;
  • token exchange and assigned_instagram_accounts discovery;
  • subscribed_apps webhook registration;
  • inbound Instagram DM and outbound reply;
  • conversation/history operations;
  • Reel comment automation with a public reply and a private flow.

Meta references

@github-actions github-actions Bot added the feature New feature or request label Nov 13, 2026
@sung17

sung17 commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the detailed issue and for putting this together. Gating the new flow behind an optional unifiedLoginConfigId, with no migration, is the right approach. We'd like to merge it once a few things are addressed:

1. The legacy flow changes after all (multiAccount: false → true)
integrations/instagram/src/integration.ts now sets multiAccount: true for every Instagram connection, including the direct flow. In apps/builder/src/app/integrations/[...integration]/callback.ts (around line 368), a non-multi-account provider finishes the connect automatically for API/MCP callers. With this change, an agent connecting a legacy Instagram channel has to make an extra targets call it doesn't make today. AI agents are the primary users of the connect API, so we need the legacy path to behave exactly as before. Could you either keep auto-completion when there is exactly one selectable candidate, or limit multi-account selection to the Unified flow?

2. Hardcoded UI label
"Unified Login Configuration ID" in instagram-settings.tsx needs to go through useTranslations(), with the key added to every file under apps/builder/messages/.

3. Unrelated version changes

  • The form default changes from v25.0 to v27.0. Please revert it or move it to a separate PR.
  • exchangeUnifiedCodeForToken and getUnifiedInstagramAccounts hardcode "v22.0". Please use DEFAULT_API_VERSION from src/constants.ts.

4. Token type assumption
refreshAuth returns Unified auth unchanged, which assumes the code exchange always returns a non-expiring Business Integration System User token. If the Login for Business configuration is set to User access token, the token will expire and the channel will stop working without any visible error. Could you either detect the token type at exchange time (for example with debug_token) and fail clearly or handle refresh, or document that the configuration must use a system-user token? A short admin note on creating the config_id would help either way.

5. Things only a live test can confirm
These look reasonable but are assumptions until the smoke test runs:

  • Webhook routing uses entry.id. Does it match the id returned by me/assigned_instagram_accounts, which is now stored as igId/pageId? If they differ, inbound messages won't find the channel.
  • /{ig-id}/messages, /{ig-id}/messenger_profile, conversation sync and subscribed_apps (sent as query params) on graph.facebook.com with this token.

Please share the results of the end-to-end test (OAuth → account selection → webhook subscription → inbound message → reply → history sync) once you've run it.

6. Docs
The new credential field should be reflected in the CLI/MCP docs and the Instagram setup docs.

Happy to help test once these are in. Thanks again!

…d-login

# Conflicts:
#	apps/builder/src/features/contacts/lib/profile-fetcher-factories.ts
@gitpascal

Copy link
Copy Markdown
Author

Implemented the requested follow-up in c866181 and merged the current main in b748a798.

Changes addressing the review:

  • Preserved the legacy one-account flow: API/MCP OAuth sessions now auto-connect whenever exactly one target is selectable, even if the provider supports multiple accounts. Multiple selectable targets remain in awaiting_selection. Builder sessions still keep their explicit selection/confirmation UI.
  • Added regression coverage for one selectable plus one unavailable target and for multiple selectable targets.
  • Moved the Unified Login Configuration ID label to useTranslations and added the key to every locale.
  • Reverted the unrelated UI default from Graph API v27.0 to v25.0.
  • Replaced the hard-coded Unified Login v22.0 fallbacks with DEFAULT_API_VERSION.
  • Documented the token contract: Unified Login must issue a non-expiring Business Integration System User token. The current integration does not rotate expiring system-user tokens; an expired token requires reconnecting after the Meta configuration is updated.
  • Added Instagram setup documentation plus CLI/MCP connection behavior and commands.
  • Kept the direct Instagram Login path unchanged when unifiedLoginConfigId is empty.
  • Fixed the Instagram/Facebook handler typing exposed by the full builder typecheck.

Validation after merging the current base branch:

  • Instagram integration: 32 test files / 144 tests passed.
  • Builder connection/API tests: 3 files / 109 tests passed.
  • CLI collision/smoke suite after build: 5 files / 60 tests passed.
  • Builder and Instagram TypeScript checks passed.
  • Translation validation and Ultracite checks passed.

Live self-hosted validation also passed end to end:

  • Meta Unified Login OAuth and account selection
  • token exchange and account discovery
  • subscribed_apps webhook registration
  • inbound Instagram DM and outbound reply
  • conversation/history operations
  • Reel comment automation: public comment reply plus private flow

The PR description has been updated with these results and the branch is conflict-free with the current main.

@gitpascal
gitpascal marked this pull request as ready for review November 16, 2026 14:51
Deathgiver and others added 3 commits November 26, 2026 10:05
Resolve conflicts with ChatbotXIO#1769, ChatbotXIO#1776 and ChatbotXIO#1783:
- keep grantedScopes from the direct code exchange next to loginType
- route main's new post listing/management calls (me/media, me/stories,
  managed media) through the login-type-aware Graph client
- keep the AhaChat import availability wrapper on the Instagram picker
- merge the connectSessions.get description with main's selectable guidance

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ublishing

Unified Login channels still reached graph.instagram.com, which rejects the
system-user token, in several paths:

- coexist history sync and contact scan: pass loginType to the sync APIs
- multi-account picker: connect through the direct-login route; the
  instagram-facebook route rejects an `instagram` connect session
- Reconnect button (ChatbotXIO#1783): open Unified Login when the credential has a
  configuration, exchange the code on graph.facebook.com, match the stored
  igId among the assigned accounts and keep loginType
- live_comments subscription: pass loginType
- post publishing: publish Unified inboxes with the graph.facebook.com
  publisher (isInstagramOnFacebookGraph)

Also fix the select-page test that broke when the page started using
getTranslations, and cover each path above with tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sung17

sung17 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the follow-up. We went through the branch again in detail and, to save a round trip, pushed the fixes directly (maintainer edits). Could you please pull and re-check, ideally with one more live run on your Unified Login app?

What we pushed

  1. 099509042 merges main (it had conflicted again after feat(posts): add post management, publishing, scheduling and feeds #1769, feat(posts): keep post counters fresh and re-check publishing permissions #1776 and fix(instagram): allow disconnecting and reconnecting a channel #1783). Conflict resolutions only:
    • keep grantedScopes from the direct code exchange next to loginType;
    • route main's new post listing/management calls (me/media, me/stories, managed media) through getInstagramGraphClient / getInstagramAccountNode.
  2. 5a9fb870a fixes the places where a Unified channel still reached graph.instagram.com with the system-user token:
    • History sync and contact scan (worker). coexist/instagram-adapter.ts and contact-scan/adapters/instagram.ts never passed loginType to the sync APIs.
    • Picking from several accounts. With more than one assigned account, SelectFacebookAccounts posted to /channels/instagram-facebook/connect, which rejects an instagram connect session. It now takes a provider prop and uses the direct-login route. A single account was unaffected.
    • Reconnect button (from fix(instagram): allow disconnecting and reconnecting a channel #1783). It always opened instagram.com/oauth/authorize, which gives "Invalid platform app" for a Meta app id. It also exchanged the code the direct way and dropped loginType. It now opens Unified Login when the credential has a configuration, exchanges on graph.facebook.com, matches the stored igId among assigned_instagram_accounts and keeps loginType: "unified".
    • live_comments subscription. ensure-live-comments-subscription.ts did not pass loginType.
    • Post publishing (from feat(posts): add post management, publishing, scheduling and feeds #1769). A Unified inbox got the instagram publisher (graph.instagram.com). It now uses the graph.facebook.com publisher via the new isInstagramOnFacebookGraph.
    • instagram-select-page.test.tsx failed on this branch once the page started calling getTranslations. The test is fixed, and every path above has a test.
  3. c9ff940fc merges the latest main (no conflicts).

Checks run: pnpm lint (including check:invariants), check-types for builder, worker, database, utils and integration-instagram, plus the related suites: builder 117 files / 1,185 tests, worker 24 / 326, business 32 / 612, integration-instagram 39 / 285.

Please verify on a live Unified Login app

  • Connect when the token has 2+ assigned accounts, then pick one or several
  • Enable history sync on a Unified channel and check that older conversations are imported (worker job)
  • Contact scan on a Unified channel
  • Reconnect a Unified channel from Settings → Channels
  • Publish an image post (and a Reel if possible) to a Unified channel
  • Enable a live-comment automation (live_comments subscription)
  • Your earlier flow still works: one account, inbound DM → reply, comment automation

One open question from the first review: please confirm that entry.id on inbound Unified webhooks equals the id returned by me/assigned_instagram_accounts.

Also noting for the record: after this PR, the auto-complete in callback.ts (finish when exactly one target is selectable) applies to every provider for API/MCP callers, not only Instagram. We're fine with that.

If anything looks wrong, push a fix on top or tell us and we'll adjust. Thanks!

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Instagram channel cannot connect Meta apps that require Unified Login

2 participants