PHPackages                             smarterqueue/threads-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. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. smarterqueue/threads-api

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

smarterqueue/threads-api
========================

Provides a wrapper around Meta's Threads OAuth and API

0.2.0(1y ago)4606↓61.7%1MITPHPPHP &gt;=8.1

Since Jul 15Pushed 1y ago1 watchersCompare

[ Source](https://github.com/SmarterQueue/Threads-API-PHP)[ Packagist](https://packagist.org/packages/smarterqueue/threads-api)[ Docs](https://smarterqueue.com)[ RSS](/packages/smarterqueue-threads-api/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (4)Versions (6)Used By (0)

Threads API Wrapper for PHP
===========================

[](#threads-api-wrapper-for-php)

This provides a PHP wrapper around [Meta's Threads API](https://developers.facebook.com/docs/threads/).

Usage
=====

[](#usage)

There are 2 main API service classes - the `ThreadsApi` and `ThreadsOAuthHelper`

- `ThreadsApi` - The base class for sending requests and parsing the response &amp; handling errors
- `ThreadsOAuthHelper` - A helper class with methods to:
    - Build a login URL
    - Exchange the authorization code for a short-lived token
    - Exchange a short-lived token for a long-lived token
    - Refresh a long-lived token

On successful responses, the API services will return a `ThreadsResponse` object that will contain the http code, headers, and decoded data.

On unsuccessful requests or PHP exceptions, the API services will throw a `ThreadsException` object that will contain the message, http code, status code (if any), sub code (if any), error type (if any), fb trace id (if any), and the previous exception (if any).

Create a link to the login page
-------------------------------

[](#create-a-link-to-the-login-page)

```
use SmarterQueue\ThreadsApi;
use SmarterQueue\ThreadsOAuthHelper;

// Set up the API instances.
$threadsApi = new ThreadsApi($clientId, $clientSecret);
$threadsOAuthHelper = new ThreadsOAuthHelper($threadsApi);

// Setup CSRF protection.
$state = bin2hex(random_bytes(32));
$_SESSION['threads_state'] = $state;

// Setup scopes and callback.
$scopes = ['threads_basic', 'threads_content_publish'];
$redirectUri = 'https://my-site.com/oauth-callback';

// Generate login URL & redirect to it.
$loginUrl = $threadsOAuthHelper->getLoginUrl($scopes, $redirectUri, $state);
header('Location: ' . $loginUrl);
```

Exchange code for access tokens
-------------------------------

[](#exchange-code-for-access-tokens)

```
use SmarterQueue\ThreadsApi;
use SmarterQueue\ThreadsOAuthHelper;

// Set up the API instances.
$threadsApi = new ThreadsApi($clientId, $clientSecret);
$threadsOAuthHelper = new ThreadsOAuthHelper($threadsApi);

// GET params.
$code = $_GET['code'] ?? null;
$state = $_GET['state'] ?? null;

// CSRF Check.
if (!isset($_SESSION['threads_state']) || $state === null || $state !== $_SESSION['threads_state'])
{
	throw new \Exception('CSRF Error - State mismatch');
}

// Exchange authorization code for short-lived token.
$redirectUri = 'https://my-site.com/oauth-callback';
$response = $threadsOAuthHelper->getShortLivedAccessToken($code, $redirectUri);
$shortLivedToken = $response->decodedData['access_token'];

// Exchange short-lived token for long-lived token.
$response = $threadsOAuthHelper->getLongLivedAccessToken($shortLivedToken);
$longLivedToken = $response->decodedData['access_token'];
```

Refresh the long-lived access token
-----------------------------------

[](#refresh-the-long-lived-access-token)

```
use SmarterQueue\ThreadsApi;
use SmarterQueue\ThreadsOAuthHelper;

// Set up the API instances.
$threadsApi = new ThreadsApi($clientId, $clientSecret);
$threadsOAuthHelper = new ThreadsOAuthHelper($threadsApi);
$longLivedToken = 'Replace with your token here';
$threadsApi->setAccessToken($longLivedToken);

// Refresh the token.
$response = $threadsOAuthHelper->refreshLongLivedAccessToken($longLivedToken);
$refreshedToken = $response->decodedData['access_token'];
```

General requests
----------------

[](#general-requests)

```
use SmarterQueue\ThreadsApi;
use SmarterQueue\ThreadsOAuthHelper;

// Set up the API instance.
$threadsApi = new ThreadsApi($clientId, $clientSecret);
$longLivedToken = 'Replace with your token here';
$threadsApi->setAccessToken($longLivedToken);

// Get my details
$response = $threadsApi->get('me', ['fields' => 'id,username,threads_profile_picture_url']);

// Publish a post
$containerResponse = $threadsApi->post('me/threads', ['media_type' => 'TEXT', 'text' => "Test post"]);
$threadsApi->post('me/threads_publish', ['creation_id' => $containerResponse->decodedData['id']]);
```

Handling errors
---------------

[](#handling-errors)

```
try {
  $containerResponse = $threadsApi->post('me/threads', ['media_type' => 'TEXT', 'text' => "Test post"]);
} catch (ThreadsApiException $e) {
  $logger->log('Error getting my details', [
    'message' => $e->getMessage(),
    'code' => $e->getCode(),
    'subCode' => $e->subcode,
    'httpCode' => $e->httpCode,
  ]);
}
```

---

By [SmarterQueue](https://smarterqueue.com)

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity43

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 ~0 days

Total

2

Last Release

716d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ade899d477b0527e905d4d00f863ab43388778a6fa0dea33719153950f2e9cc6?d=identicon)[mbreden](/maintainers/mbreden)

---

Top Contributors

[![MattBred](https://avatars.githubusercontent.com/u/17261474?v=4)](https://github.com/MattBred "MattBred (10 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/smarterqueue-threads-api/health.svg)

```
[![Health](https://phpackages.com/badges/smarterqueue-threads-api/health.svg)](https://phpackages.com/packages/smarterqueue-threads-api)
```

###  Alternatives

[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.3k543.5M2.6k](/packages/aws-aws-sdk-php)[neuron-core/neuron-ai

The PHP Agentic Framework.

2.0k656.1k38](/packages/neuron-core-neuron-ai)[tencentcloud/tencentcloud-sdk-php

TencentCloudApi php sdk

3741.3M47](/packages/tencentcloud-tencentcloud-sdk-php)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[ellaisys/aws-cognito

Laravel Authentication using AWS Cognito (Web and API)

123256.9k1](/packages/ellaisys-aws-cognito)[eslazarev/wildberries-sdk

Wildberries OpenAPI clients (generated).

273.0k](/packages/eslazarev-wildberries-sdk)

PHPackages © 2026

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