A PHP client for the Hetzner Cloud API. The goal of this project is to provide an easy-to-use and framework agnostic PHP PHP client for projects and applications interacting with Hetzner Cloud. Some use cases might include:
- Getting metrics for all servers within a project
- Programmatically creating firewalls
- Searching for available datacenters
The client is available as a composer packager that can be installed in any project using composer:
composer require hetzner-cloud-php/client
Since the client is compatible with any PSR-18 HTTP client, any commonly used HTTP client can be used thanks
to our dependency on php-http/discovery
. Once both dependencies have been installed, you may start interaction
with Hetzner Cloud API:
/** @var string $apiKey */
$apiKey = $_ENV['HETZNER_CLOUD_API_KEY'];
$client = HetznerCloud::client($apiKey);
// Create a server
$response = $createdServer = $client->servers()->createServer([
'name' => 'test-server',
'server_type' => 'cpx11',
'image' => 'ubuntu-24.04',
]);
echo $response->server->name; // 'test-server'
For a comprehensive set of examples, take a look at the examples directory.
Gets a list of available datacenters.
$response = $client->datacenters()->getDatacenters(sort: 'name:desc');
echo $response->datacenters // array<int, Datacenter>
echo $response->meta // Meta::class
echo $response->toArray() // ['datacenter' => ['id' => 42, ...], 'meta' => [...]]
Gets a single datacenter.
$response = $client->datacenters()->getDatacenter(42);
echo $response->datacenter // Datacenter::class
echo $response->toArray() // ['datacenter' => ['id' => 42, ...], 'error' => null]
Retrieves a single firewall for a project.
$response = $client->firewalls()->getFirewall(1337);
$response->firewall; // Firewall::class
$response->toArray(); // ['firewall' => ['id => 1337', ...]]
Retrieves all firewalls for a project, with a few optional query parameters.
$response = $client->firewalls()->getFirewalls(name: 'coolify', labelSelector: 'foo');
$response->firewalls; // array<int, Firewall::class>
foreach ($response->firewalls as $firewall) {
$firewall->id;
$firewall->name;
// ...
}
$response->toArray(); // ['firewalls' => [...], 'meta' => [...]]
Gets a list of all existing servers for a project.
$response = $client->servers()->getServers(sort: 'name:asc');
$response->servers; // array<int, Server>
$response->toArray(); // ['servers' => ['id' => 42, ...], 'meta' => [...]]
TODO
TODO
TODO
TODO
TODO