1. Go to this page and download the library: Download boci/hetzner-laravel 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/ */
use Boci\HetznerLaravel\Testing\ClientFake;
use Boci\HetznerLaravel\Responses\Servers\CreateResponse;
$client = new ClientFake([
CreateResponse::fake([
'name' => 'test-server',
'server_type' => 'cpx11',
]),
]);
$response = $client->servers()->create([
'name' => 'test-server',
'server_type' => 'cpx11',
'image' => 'ubuntu-24.04',
'location' => 'nbg1',
]);
expect($response->server()->name())->toBe('test-server');
// Assert that requests were sent
$client->assertSent(\Boci\HetznerLaravel\Resources\Servers::class, function (string $method, array $parameters): bool {
return $method === 'create' && $parameters['name'] === 'test-server';
});
use Boci\HetznerLaravel\Testing\ClientFake;
use Boci\HetznerLaravel\Exceptions\ErrorException;
$client = new ClientFake([
new ErrorException([
'message' => 'Server not found',
'code' => 'server_not_found',
], 404)
]);
// This will throw the ErrorException
$client->servers()->retrieve('non-existent');
use Boci\HetznerLaravel\Client;
use GuzzleHttp\Client as GuzzleClient;
$client = Client::factory()
->withApiKey('your-api-token')
->withHttpClient(new GuzzleClient([
'timeout' => 60,
'verify' => false, // Only for development
]))
->make();
use Boci\HetznerLaravel\Client;
class ServerController
{
public function __construct(
private Client $hetznerClient
) {}
public function createServer()
{
$response = $this->hetznerClient->servers()->create([
'name' => 'my-server',
'server_type' => 'cpx11',
'image' => 'ubuntu-24.04',
'location' => 'nbg1',
]);
return response()->json($response->toArray());
}
}