API Client for managing Hetzner's Cloud
Install this package via composer composer install webfoersterei/hetzner-cloud-api-client
Note: There is no stable release at the moment
After that you can use the client like this:
define('API_KEY', 'MYSECRETAPIKEY'); # See https://docs.hetzner.cloud/#header-authentication-1
$cloudApiClient = Webfoersterei\HetznerCloudApiClient\ClientFactory::create(API_KEY);
# You can now use the cloudApiClient. E.g:
$cloudApiClient->getServers();
The client provides implementations for the following methods (grouped by resources).
See: https://docs.hetzner.cloud/#resources-actions
- List all:
getActions()
- Get one:
getAction(int $id)
See: https://docs.hetzner.cloud/#resources-servers
- Get all:
getServers()
- Get one:
getServer(int $id)
- Create one:
createServer(CreateRequest $createRequest)
- Rename:
changeServerName(int $id, string $name)
- Delete one:
deleteServer(int $id)
See: https://docs.hetzner.cloud/#resources-server-types
- Get all:
getServerTypes()
- Get one:
getServerType(int $id)
See: https://docs.hetzner.cloud/#resources-pricing-get
- Get all:
getPricing()
Feel free to contribute to this github repository by reporting bugs and ideas to improve this API-client.
Lists all serverTypes: name and specs
# Require vendors/autoload.php here
define('API_KEY', 'MYSECRETAPIKEY');
$client = \Webfoersterei\HetznerCloudApiClient\ClientFactory::create(API_KEY);
$serverTypes = $client->getServerTypes();
foreach ($serverTypes->server_types as $serverType) {
printf("%--12s CPU: %2d Cores, RAM: %3d GB, Storage: %4d GB (%s)\n", $serverType->name, $serverType->cores,
$serverType->memory, $serverType->disk, $serverType->storage_type);
}
Create a new server and delete it after started
# Require vendors/autoload.php here
define('API_KEY', 'MYSECRETAPIKEY');
$client = \Webfoersterei\HetznerCloudApiClient\ClientFactory::create(API_KEY);
$createServerRequest = new \Webfoersterei\HetznerCloudApiClient\Model\Server\CreateRequest();
$createServerRequest->name = 'my-first-server-created-with-php';
$createServerRequest->server_type = 'cx11';
$createServerRequest->start_after_create = true;
$createServerRequest->image = 1;
echo "Creating server.\n";
$createResponse = $client->createServer($createServerRequest);
$progress = $createResponse->action->progress;
$status = $createResponse->action->status;
while ($progress != 100 && $status != 'success') {
$actionResponse = $client->getAction($createResponse->action->id);
echo $actionResponse->action->progress."%\n";
$progress = $actionResponse->action->progress;
sleep(1);
}
echo sprintf("Server created. Root-PW: %s\n", $createResponse->root_password);
sleep(10);
echo "Deleting server.\n";
$client->deleteServer($createResponse->server->id);
echo "Done.\n";