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)4515↓25%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 1mo ago

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

29

—

LowBetter than 59% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity42

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

671d 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

[josiasmontag/laravel-recaptchav3

Recaptcha V3 for Laravel package

2641.6M2](/packages/josiasmontag-laravel-recaptchav3)[rahul900day/laravel-captcha

Different types of Captcha implementation for Laravel Application.

10715.9k](/packages/rahul900day-laravel-captcha)[simplesamlphp/simplesamlphp-module-oidc

A SimpleSAMLphp module adding support for the OpenID Connect protocol

5016.9k1](/packages/simplesamlphp-simplesamlphp-module-oidc)[kinde-oss/kinde-auth-php

Kinde PHP SDK for authentication

2369.5k3](/packages/kinde-oss-kinde-auth-php)[descope/descope-php

Descope SDK for PHP

3814.0k](/packages/descope-descope-php)[njoguamos/laravel-turnstile

A laravel wrapper for https://developers.cloudflare.com/turnstile/

2315.9k2](/packages/njoguamos-laravel-turnstile)

PHPackages © 2026

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