PHPackages                             texhub/instagram-graph-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. texhub/instagram-graph-api

ActiveLibrary[API Development](/categories/api)

texhub/instagram-graph-api
==========================

Full-featured Instagram Graph API SDK for any PHP framework with first-class Laravel support: OAuth &amp; long-lived tokens, content publishing, stories, comments, direct messaging with buttons, user info and webhooks.

v1.1.1(1mo ago)02MITPHPPHP ^8.2

Since Jun 1Pushed 1mo agoCompare

[ Source](https://github.com/TexhubPro/instagram-graph-api)[ Packagist](https://packagist.org/packages/texhub/instagram-graph-api)[ Docs](https://texhub.pro)[ RSS](/packages/texhub-instagram-graph-api/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (4)Versions (4)Used By (0)

TexHub · Instagram Graph API
============================

[](#texhub--instagram-graph-api)

**English** · [Русский](README.ru.md)

[![License: MIT](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)[![PHP](https://camo.githubusercontent.com/d91d3d1139cf0d8faaa80eeeeac7d3c59c9319e56960ef81c948e4160be4c4c1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e322d3737376262342e737667)](composer.json)[![Laravel](https://camo.githubusercontent.com/3e9242504354dbb5afd360f9a1ec114126b2caddb73d9e789d9102c3285d2b05/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c61726176656c2d3131253230253743253230313225323025374325323031332d6666326432302e737667)](#laravel)

A complete, framework-agnostic PHP SDK for the **Instagram Graph API** (Instagram API with Instagram Login) — OAuth &amp; long-lived tokens, content publishing, stories, comments, **direct messaging with buttons**, user info and **webhooks** — with first-class **Laravel** support.

> Everything you need to integrate Instagram, from connecting to every feature.

Reference:

---

What's covered
--------------

[](#whats-covered)

AreaMethods**OAuth &amp; tokens**authorization URL, short-lived → **long-lived (60-day)** → **refresh****User info**profile, **avatar (profile picture)**, followers/media counts**Publishing**photos, **reels/video**, **stories**, **carousels**, publish, list, get**Comments**list, comment, **reply**, hide/unhide, enable/disable, delete**Messaging**text, image, **buttons**, **quick replies**, reactions, typing, conversations**Webhooks**verify challenge, **verify `X-Hub-Signature-256`**, parse comment &amp; DM events**Escape hatch**`->http()` to call *any* Graph endpoint---

Installation
------------

[](#installation)

```
composer require texhub/instagram-graph-api
```

Requirements: **PHP ≥ 8.2** with `curl`, `json` and `hash`.

---

1. Connect (OAuth &amp; tokens)
-------------------------------

[](#1-connect-oauth--tokens)

```
use TexHub\InstagramGraphApi\Instagram;

$ig = Instagram::make('APP_ID', 'APP_SECRET');

// a) Send the user to authorize:
$url = $ig->oauth()->authorizationUrl(
    scopes: ['instagram_business_basic', 'instagram_business_content_publish',
             'instagram_business_manage_comments', 'instagram_business_manage_messages'],
    state: 'csrf-token',
    redirectUri: 'https://shop.tj/instagram/callback',
);
// redirect($url)

// b) On the callback, exchange the ?code= for tokens:
$short = $ig->oauth()->requestShortLivedToken($_GET['code'], 'https://shop.tj/instagram/callback');
$long  = $ig->oauth()->exchangeForLongLivedToken($short->token); // valid ~60 days

echo $long->token;
echo $long->expiresAt();        // unix timestamp
$long->expiresWithinDays(7);    // refresh soon?

// c) Refresh before it expires:
$refreshed = $ig->oauth()->refreshLongLivedToken($long->token);
```

Then use the token for API calls:

```
$ig = Instagram::make('APP_ID', 'APP_SECRET', accessToken: $long->token, igUserId: '17841...');
// or, from an existing instance:
$ig = $ig->withAccessToken($long->token);
```

---

2. User info &amp; avatar
-------------------------

[](#2-user-info--avatar)

```
$me = $ig->users()->me();
$me->get('username');
$me->get('followers_count');

$ig->users()->avatarUrl();   // profile picture URL
$ig->users()->username();
```

---

3. Publish content
------------------

[](#3-publish-content)

```
// Photo (create container + publish in one call):
$ig->media()->publishPhoto('https://cdn.shop.tj/photo.jpg', 'Новинка! 🔥');

// Reel:
$ig->media()->publishReel('https://cdn.shop.tj/reel.mp4', 'Смотрите 👀');

// Story:
$ig->media()->publishStory('https://cdn.shop.tj/story.jpg');

// Carousel (multi-image post):
$a = $ig->media()->createCarouselItem('https://cdn/1.jpg')->id();
$b = $ig->media()->createCarouselItem('https://cdn/2.jpg')->id();
$carousel = $ig->media()->createCarousel([$a, $b], 'Подборка')->id();
$ig->media()->publish($carousel);

// Read:
$ig->media()->list();
$ig->media()->get($mediaId);
$ig->media()->publishingLimit();   // daily quota usage
```

> Media URLs must be publicly reachable — Instagram fetches them.

---

4. Comments
-----------

[](#4-comments)

```
$ig->comments()->forMedia($mediaId);             // list comments
$ig->comments()->commentOnMedia($mediaId, 'Спасибо за внимание!');
$ig->comments()->reply($commentId, 'Ответ на комментарий');
$ig->comments()->hide($commentId);               // hide / unhide
$ig->comments()->delete($commentId);
```

---

5. Direct messaging (with buttons)
----------------------------------

[](#5-direct-messaging-with-buttons)

The recipient id is the **IGSID** you receive in messaging webhooks.

```
use TexHub\InstagramGraphApi\Builders\Button;

$ig->messages()->sendText($igsid, 'Привет! Чем помочь?');
$ig->messages()->sendImage($igsid, 'https://cdn/promo.jpg');

// Buttons:
$ig->messages()->sendButtons($igsid, 'Выберите действие:', [
    Button::url('Открыть сайт', 'https://texhub.pro'),
    Button::postback('Связаться', 'CONTACT'),
]);

// Quick replies:
$ig->messages()->sendQuickReplies($igsid, 'Ваш выбор?', [
    Button::quickReply('Да', 'YES'),
    Button::quickReply('Нет', 'NO'),
]);

// Typing indicator / read receipts / reactions:
$ig->messages()->senderAction($igsid, 'typing_on');
$ig->messages()->react($igsid, $messageId, 'love');

// Conversations & history:
$ig->messages()->conversations();
$ig->messages()->messages($conversationId);
```

---

6. Webhooks
-----------

[](#6-webhooks)

**Verification (GET)** — echo the challenge:

```
$challenge = $ig->webhooks()->verifyChallenge($_GET);
if ($challenge !== null) { echo $challenge; exit; } // HTTP 200
```

**Events (POST)** — verify the signature, then parse:

```
$raw = file_get_contents('php://input');
$ig->webhooks()->assertValidSignature($raw, $_SERVER['HTTP_X_HUB_SIGNATURE_256'] ?? null);

foreach ($ig->webhooks()->parse($raw) as $event) {
    if ($event->isMessage()) {
        $ig->messages()->sendText($event->senderId(), 'Получили: ' . $event->messageText());
    }
    if ($event->isComment()) {
        // $event->get('id'), $event->get('text'), ...
    }
}
http_response_code(200);
```

---

Error handling
--------------

[](#error-handling)

```
use TexHub\InstagramGraphApi\Exceptions\ApiException;

try {
    $ig->media()->publishPhoto($url, $caption);
} catch (ApiException $e) {
    $e->httpStatus; $e->errorCode; $e->errorType; $e->errorSubcode; $e->fbtraceId;
    $e->isTokenError();   // code 190 — token expired/invalid
    $e->isRateLimit();
}
```

Any endpoint (escape hatch)
---------------------------

[](#any-endpoint-escape-hatch)

```
$ig->http()->get('17841.../insights', ['metric' => 'impressions,reach']);
$ig->http()->post($mediaId, ['comment_enabled' => 'false']);
```

---

 Laravel
-------------------------------------------

[](#-laravel)

Auto-discovered. Publish config:

```
php artisan vendor:publish --tag=instagram-config
```

`.env`:

```
INSTAGRAM_APP_ID=...
INSTAGRAM_APP_SECRET=...
INSTAGRAM_ACCESS_TOKEN=long-lived-token
INSTAGRAM_USER_ID=17841...
INSTAGRAM_REDIRECT_URI=https://shop.tj/instagram/callback
INSTAGRAM_WEBHOOK_VERIFY_TOKEN=your-verify-token
INSTAGRAM_API_VERSION=v23.0
```

Facade:

```
use TexHub\InstagramGraphApi\Laravel\Instagram;

Instagram::media()->publishPhoto($url, 'Привет из Laravel!');
Instagram::messages()->sendText($igsid, 'Ответ');
```

### Webhook controller

[](#webhook-controller)

```
public function verify(Request $request) {
    return response(Instagram::webhooks()->verifyChallenge($request->query()) ?? '', 200);
}

public function handle(Request $request) {
    Instagram::webhooks()->assertValidSignature(
        $request->getContent(),
        $request->header('X-Hub-Signature-256'),
    );

    foreach (Instagram::webhooks()->parse($request->getContent()) as $event) {
        // ...
    }
    return response('', 200);
}
```

---

Multi-tenant / SaaS
-------------------

[](#multi-tenant--saas)

Many customers can connect **their own** Instagram accounts through one Meta app. Each tenant authorizes via OAuth and gets their own long-lived token; one webhook URL serves everyone.

```
// Onboarding: each tenant runs the OAuth flow → store their long-lived token.
$long = $ig->oauth()->exchangeForLongLivedToken($short->token);
// → save {token, user_id} for this tenant

// Act as any tenant — bind a client to their stored token:
$ig->withAccessToken($tenant->ig_token)->media()->publishPhoto($url, $caption);

// One webhook for everyone — route by the account that received it:
foreach ($ig->webhooks()->parse($raw) as $event) {
    $tenant = Tenant::where('ig_account_id', $event->accountId())->first();
    if ($event->isMessage()) { /* $event->senderId(), $event->messageText() */ }
}
```

`$event->accountId()` (the connected account / `entry.id`) and `$event->recipientId()` are the tenant routing keys. Signatures are verified with your single app secret.

Testing
-------

[](#testing)

```
use TexHub\InstagramGraphApi\Instagram;
use TexHub\InstagramGraphApi\Config;
use TexHub\InstagramGraphApi\Tests\Support\FakeTransport;

$t = (new FakeTransport())->push(['id' => 'MEDIA_1']);
$ig = new Instagram(new Config('APP', 'SECRET', accessToken: 'TOKEN'), $t);
// assert on $t->history / $t->lastUrl()
```

```
composer install
composer test
```

---

Architecture
------------

[](#architecture)

```
src/
├── Instagram.php            # entry — oauth()/webhooks()/users()/media()/comments()/messages()
├── Config.php               # immutable configuration
├── Http/                    # Transport, CurlTransport, HttpClient, RawResponse
├── OAuth/                   # OAuthClient, AccessToken
├── Webhook/                 # WebhookHandler (challenge + signature + parse), WebhookEvent
├── Resources/               # Users, Media, Comments, Messages
├── Builders/                # Message, Button (buttons & quick replies)
├── Responses/               # Response (ArrayAccess), ListResponse
├── Exceptions/              # ApiException, TransportException, …
└── Laravel/                 # ServiceProvider + Facade

```

---

License
-------

[](#license)

MIT © TexHub Pro — built by Mahmudi Shodmehr.

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance93

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~10 days

Total

3

Last Release

34d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/217647751?v=4)[TexHub Pro](/maintainers/TexhubPro)[@TexhubPro](https://github.com/TexhubPro)

---

Top Contributors

[![TexhubPro](https://avatars.githubusercontent.com/u/217647751?v=4)](https://github.com/TexhubPro "TexhubPro (3 commits)")

---

Tags

instagraminstagram-graph-apilaravelmessagingmetaphpsdkwebhooksphplaravelinstagrammessagingwebhooksmetainstagram graph apiinstagram apitexhub

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/texhub-instagram-graph-api/health.svg)

```
[![Health](https://phpackages.com/badges/texhub-instagram-graph-api/health.svg)](https://phpackages.com/packages/texhub-instagram-graph-api)
```

###  Alternatives

[jstolpe/instagram-graph-api-php-sdk

Instagram Graph API PHP SDK

138110.7k2](/packages/jstolpe-instagram-graph-api-php-sdk)[edbizarro/laravel-facebook-ads

Facebook &amp; Instagram Ads for Laravel 5.6+

13629.9k](/packages/edbizarro-laravel-facebook-ads)[scriptdevelop/whatsapp-manager

Paquete para manejo de WhatsApp Business API en Laravel

783.8k](/packages/scriptdevelop-whatsapp-manager)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
