A PHP client library for the Neon API.
neon-api-php-sdk is a PHP wrapper designed to simplify interactions with the Neon API.
It provides a convenient way for developers to integrate their PHP applications
with the Neon platform, offering methods to manage API keys, projects, branches,
databases, endpoints, roles, and operations programmatically.
- PHP 8.4 or higher
- Composer
- PSR
You can install the package via Composer:
composer require dragonwize/neon-api-sdk<?php
use Dragonwize\NeonApiSdk\Client\NeonApi;
// Initialize the client with your API key
$neon = new NeonApi('your_api_key');Important: Never expose your API key in your code. Always use environment variables or secure configuration management.
me(): Returns the current user information.
apiKeys(): Returns a list of API keys.createApiKey(array $data): Creates a new API key.revokeApiKey(string $apiKeyId): Revokes an API key.
projects(bool $shared = false, ?string $cursor = null, ?int $limit = null): Returns a list of projects.project(string $projectId): Returns a specific project.createProject(array $data): Creates a new project.updateProject(string $projectId, array $data): Updates a project.deleteProject(string $projectId): Deletes a project.connectionUri(string $projectId, ?string $databaseName = null, ?string $roleName = null, bool $pooled = false): Returns the connection URI for a project.
branches(string $projectId, ?string $cursor = null, ?int $limit = null): Returns a list of branches for a project.branch(string $projectId, string $branchId): Returns a specific branch.createBranch(string $projectId, array $data): Creates a new branch.updateBranch(string $projectId, string $branchId, array $data): Updates a branch.deleteBranch(string $projectId, string $branchId): Deletes a branch.setBranchAsPrimary(string $projectId, string $branchId): Sets a branch as primary.
databases(string $projectId, string $branchId, ?string $cursor = null, ?int $limit = null): Returns a list of databases.database(string $projectId, string $branchId, string $databaseId): Returns a specific database.createDatabase(string $projectId, string $branchId, array $data): Creates a new database.updateDatabase(string $projectId, string $branchId, string $databaseId, array $data): Updates a database.deleteDatabase(string $projectId, string $branchId, string $databaseId): Deletes a database.
endpoints(string $projectId): Returns a list of endpoints.endpoint(string $projectId, string $endpointId): Returns a specific endpoint.createEndpoint(string $projectId, array $data): Creates a new endpoint.updateEndpoint(string $projectId, string $endpointId, array $data): Updates an endpoint.deleteEndpoint(string $projectId, string $endpointId): Deletes an endpoint.startEndpoint(string $projectId, string $endpointId): Starts an endpoint.suspendEndpoint(string $projectId, string $endpointId): Suspends an endpoint.
roles(string $projectId, string $branchId): Returns a list of roles.role(string $projectId, string $branchId, string $roleName): Returns a specific role.createRole(string $projectId, string $branchId, string $roleName): Creates a new role.deleteRole(string $projectId, string $branchId, string $roleName): Deletes a role.revealRolePassword(string $projectId, string $branchId, string $roleName): Reveals a role password.resetRolePassword(string $projectId, string $branchId, string $roleName): Resets a role password.
operations(string $projectId, ?string $cursor = null, ?int $limit = null): Returns a list of operations.operation(string $projectId, string $operationId): Returns a specific operation.
<?php
use Dragonwize\NeonApiSdk\Client\NeonApi;
$neon = NeonApi::fromEnvironment();
$project = $neon->createProject([
'project' => [
'name' => 'My New Project',
'region_id' => 'aws-us-east-1',
]
]);
echo "Created project: " . $project->name . " with ID: " . $project->id;<?php
use Dragonwize\NeonApiSdk\Client\NeonApi;
$neon = NeonApi::fromEnvironment();
$response = $neon->createBranch('project-id', [
'branch' => [
'name' => 'feature-branch',
]
]);<?php
use Dragonwize\NeonApiSdk\Client\NeonApi;
$neon = NeonApi::fromEnvironment();
$connectionData = $neon->connectionUri(
'project-id',
'database-name',
'role-name'
);
echo "Connection URI: " . $connectionData['uri'];The client throws NeonApiException for API errors:
<?php
use Dragonwize\NeonApiSdk\Client\NeonApi;
use Dragonwize\NeonApiSdk\Exception\NeonApiException;
try {
$neon = NeonApi::fromEnvironment();
$project = $neon->project('invalid-project-id');
} catch (NeonApiException $e) {
echo "API Error: " . $e->getMessage();
}The library includes strongly-typed data models for API responses:
Project: Represents a Neon projectBranch: Represents a project branchDatabase: Represents a databaseEndpoint: Represents a compute endpointRole: Represents a database roleOperation: Represents an operation
These models provide type safety and IDE autocompletion.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.