# OnlyFans PHP SDK

> A typed, production-ready PHP client for the OnlyFans API. Authenticate once, call any endpoint, and ship reliable creator tools with less boilerplate.

- Requires PHP 8.1+
- Source: https://github.com/onlyfansapi/onlyfansapi-php
- Package: `onlyfansapi/onlyfansapi-php` on Packagist

## Install

```bash
composer require onlyfansapi/onlyfansapi-php
```

## Quick start

```php
<?php

use Onlyfansapi\Client;
use Onlyfansapi\Chats\ChatListParams\Order;

$client = new Client(apiKey: getenv('ONLYFANSAPI_API_KEY'));

// GET /api/{account}/chats
$chats = $client->chats->list('acct_XXXXXXXXXXXXX', limit: '10', order: Order::RECENT);

foreach ($chats->data as $chat) {
  echo "@{$chat->fan->username} · unread: {$chat->unreadMessagesCount}", PHP_EOL;
  echo "  {$chat->lastMessage->text}", PHP_EOL;
}
```

Send a message with an optional media attachment:

```php
<?php

use Onlyfansapi\Client;

$client = new Client(apiKey: getenv('ONLYFANSAPI_API_KEY'));

// POST /api/{account}/chats/{chat_id}/messages
$message = $client->chats->messages->send(
  '987654321',
  account: 'acct_XXXXXXXXXXXXX',
  text: 'Hey! Thanks for subscribing 💜',
  mediaFiles: ['ofapi_media_XXXXXXXXXXXXX'],
  price: 0,
);

echo "Message sent #{$message->data->id}", PHP_EOL;
```

Other examples on the page: link stats (cumulative clicks, subscribers, spenders, lifetime revenue for a tracking link), link spenders, chat messages (newest first), and downloading media straight from an OnlyFans CDN URL through the API's proxy and cache.

## Features

- **Modern PHP** — Built for PHP 8.1+ with current language features and conventions.
- **Configurable retries** — Tune retries globally or override them for an individual request.
- **Focused exceptions** — Handle connection, timeout, authentication, rate-limit, and API errors separately.

## Built for real products

Use one consistent client across dashboards, internal operations, and automated workflows: analytics dashboards, media workflows, internal automation, multi-account tools, reporting pipelines, and custom integrations.

## FAQ

### How do I authenticate?

Set the `ONLYFANSAPI_API_KEY` environment variable or pass `apiKey` when creating the client. Environment-based configuration keeps credentials out of source control.

### Does the SDK include request and response types?

Yes. The SDK uses named parameters and typed value objects (`::with()` constructors and builders) for requests and responses. PHP 8.1+.

### What happens when a request fails?

A subclass of `APIException` is thrown — `AuthenticationException`, `RateLimitException`, `APIConnectionException`, and more. Connection errors, timeouts, 429s, and 5xx responses are retried twice automatically with exponential backoff (configurable via `maxRetries`).

### Can I upload files?

Yes. Pass a resource from `fopen()`, a string of file contents, or a `FileParam` instance.

### Can I call an endpoint that is not documented yet?

Yes. Send undocumented parameters to any endpoint and read undocumented response properties while keeping authentication and retries.

## Links

- Get your API key: https://app.onlyfansapi.com/register
- Source on GitHub: https://github.com/onlyfansapi/onlyfansapi-php
- API reference: https://docs.onlyfansapi.com/api-reference/overview
- All SDKs: https://onlyfansapi.com/sdk