1. Go to this page and download the library: Download pooshgan/pasarguard-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pooshgan / pasarguard-php example snippets
Pooshgan\PasarGuard\Client;
use Pooshgan\PasarGuard\PasarGuard;
// 1. Initialize the HTTP client with your panel URL and admin token
$client = new Client(
baseUrl: 'https://your-panel-domain.com',
token: 'your-admin-bearer-token'
);
// 2. Wrap it with the high-level SDK facade
$api = new PasarGuard($client);
// 3. Call any endpoint group as a property
$users = $api->users->list();
print_r($users);
// 4. Create a new user with a data limit of 1 GB
$newUser = $api->users->create([
'username' => 'test_user',
'status' => 'active',
'data_limit' => 1073741824, // bytes
'expire' => (time() + 86400 * 30) * 1000, // ms since epoch
]);
echo "Created user ID: " . $newUser['id'] . PHP_EOL;
// All users (paginated)
$all = $api->users->list();
// Lightweight list (id + username only)
$simple = $api->users->listSimple();
// Filter by query string (?status=active&limit=50)
$active = $api->users->list(['status' => 'active', 'limit' => 50]);
// Users that have already expired
$expired = $api->users->getExpired();
// By username (default variant)
$u1 = $api->users->get('john_doe');
// Explicit by-username variant
$u2 = $api->users->getByUsername('john_doe');
// By internal user ID
$u3 = $api->users->getById(42);
// Fetch the subscription URL for a specific client type
$sub = $api->users->getSubscriptionById(42, 'v2rayNG');
// Create a single user
$created = $api->users->create([
'username' => 'jane_doe',
'proxies' => ['vmess' => ['id' => '...']],
'data_limit' => 5368709120, // 5 GB in bytes
'expire' => (time() + 86400*30) * 1000,
'status' => 'active',
'data_limit_reset_strategy' => 'no_reset',
]);
// Update by username
$api->users->update('jane_doe', ['data_limit' => 10737418240]);
// Update by id (also via updateByUsername / updateById)
$api->users->updateById(42, ['status' => 'disabled']);
// Delete
$api->users->delete('jane_doe');
$api->users->deleteByUsername('jane_doe');
$api->users->deleteById(42);
// Reset data usage of a single user
$api->users->resetDataUsage('jane_doe');
$api->users->resetDataUsageByUsername('jane_doe');
$api->users->resetDataUsageById(42);
// Reset data usage of EVERY user (be careful!)
$api->users->resetAllDataUsage();
// Revoke a user's subscription URL (generates a new one)
$api->users->revokeSubscription('jane_doe');
$api->users->revokeSubscriptionByUsername('jane_doe');
$api->users->revokeSubscriptionById(42);
// Transfer ownership to admin username "reseller_3"
$api->users->setOwner('jane_doe', ['admin_username' => 'reseller_3']);
$api->users->setOwnerByUsername('jane_doe', ['admin_username' => 'reseller_3']);
$api->users->setOwnerById(42, ['admin_username' => 'reseller_3']);
// Activate the user's queued "next plan"
$api->users->activeNextPlan('jane_doe');
$api->users->activeNextPlanByUsername('jane_doe');
$api->users->activeNextPlanById(42);
// Disable / activate / delete every user owned by an admin
$api->admins->disableUsers('reseller_4');
$api->admins->disableUsersByUsername('reseller_4');
$api->admins->disableUsersById(7);
$api->admins->activateUsers('reseller_4');
$api->admins->activateUsersByUsername('reseller_4');
$api->admins->activateUsersById(7);
$api->admins->deleteUsers('reseller_4');
$api->admins->deleteUsersByUsername('reseller_4');
$api->admins->deleteUsersById(7);
// Reset usage of every user owned by an admin
$api->admins->resetUsage('reseller_4');
$api->admins->resetUsageByUsername('reseller_4');
$api->admins->resetUsageById(7);
$api->admins->bulkDelete(['usernames' => ['r1', 'r2']]);
$api->admins->bulkReset(['usernames' => ['r1', 'r2']]);
$api->admins->bulkDisable(['usernames' => ['r1', 'r2']]);
$api->admins->bulkEnable(['usernames' => ['r1', 'r2']]);
$api->admins->bulkDisableUsers(['usernames' => ['r1', 'r2']]);
$api->admins->bulkActivateUsers(['usernames' => ['r1', 'r2']]);
$api->admins->bulkDeleteUsers(); // no body — deletes ALL users of EVERY admin
// List all roles
$roles = $api->adminRoles->list();
$simple = $api->adminRoles->listSimple();
// Get a role
$role = $api->adminRoles->get(1);
// Create a role
$api->adminRoles->create([
'name' => 'Support Agent',
'permissions' => ['users:read', 'users:reset'],
'color' => '#3b82f6',
]);
// Update / delete
$api->adminRoles->update(1, ['name' => 'Senior Support']);
$api->adminRoles->delete(1);
// List HWID locks of a user
$api->hwids->get(42);
// Delete a single HWID lock
$api->hwids->delete(42, 'hwid-string-from-client');
// Reset (clear) all HWID locks for a user
$api->hwids->reset(42, []);
// Get the auto-detected subscription content for a token
$api->subscriptions->get('subscription-token-here');
// Informational page (used by the panel's web UI)
$api->subscriptions->getInfo('subscription-token-here');
// Raw subscription (plain-text list of links)
$api->subscriptions->getRaw('subscription-token-here');
// App store / download page links
$api->subscriptions->getApps('subscription-token-here');
// Subscription usage statistics
$api->subscriptions->getUsage('subscription-token-here');
// Subscription content for a SPECIFIC client type (v2rayNG, Streisand, ...)
$api->subscriptions->getWithClientType('subscription-token-here', 'v2rayNG');
// Telegram Web App sends init_data in the URL fragment or Authorization header.
$initData = $_SERVER['HTTP_X_TELEGRAM_INIT_DATA'] ?? '';
$client = new Client('https://panel.example.com', ''); // no bearer needed for this route
$api = new PasarGuard($client);
$resp = $api->admins->getMiniAppToken(['init_data' => $initData]);
$token = $resp['access_token']; // use this for subsequent calls as the admin
// Now make a real client with this admin token
$adminApi = new PasarGuard(
new Client('https://panel.example.com', $token)
);
$me = $adminApi->admins->listSimple();
print_r($me);
use Pooshgan\PasarGuard\Exceptions\PasarGuardException;
try {
$api->users->get('non_existent_user');
} catch (PasarGuardException $e) {
echo $e->getMessage(); // "User not found" (server-provided detail)
echo $e->getCode(); // 404 (HTTP status)
print_r($e->getErrorData()); // full decoded JSON body from the server
}
$panelA = new PasarGuard(new Client('https://panel-a.example.com', $tokenA));
$panelB = new PasarGuard(new Client('https://panel-b.example.com', $tokenB));
$usersA = $panelA->users->list();
$usersB = $panelB->users->list();
$client = new Client(
baseUrl: 'https://panel.example.com',
token: '...',
subscriptionPath: '/custom-sub',
);
$api = new PasarGuard($client);
$api->subscriptions->get('token'); // GET /custom-sub/token
class CustomUser extends \Pooshgan\PasarGuard\Endpoints\User {
public function createForm(array $formData): array {
return $this->client->request('POST', $this->pathPrefix, [
'form_params' => $formData,
]);
}
}
public function __construct(
string $baseUrl,
string $token,
string $subscriptionPath = '/sub',
array $guzzleOptions = [],
)
public function request(string $method, string $uri, array $options = []): array
public function __construct(Client $client)
// Read (no body, optional query + headers)
public function list(array $query = [], array $headers = []): array;
// Mutate (JSON body, optional query + headers)
public function create(array $data = [], array $query = [], array $headers = []): array;
// Mutate with identifier
public function update(string $id, array $data = [], array $query = [], array $headers = []): array;
public function getMessage(): string; // server-provided detail
public function getCode(): int; // HTTP status code
public function getErrorData(): array; // decoded JSON error body