Servers are virtual machines that can be provisioned.
Get all Servers
Returns all existing Server objects
HetznerCloudClient hetznerCloudClient =newHetznerCloudClient("apiKey");// Get AllList<Server> list =awaithetznerCloudClient.Server.Get();
Get a Server
Returns a specific Server object. The Server must exist inside the Project
HetznerCloudClient hetznerCloudClient =newHetznerCloudClient("apiKey");long serverId =39447794;// Get OneServer server =awaithetznerCloudClient.Server.Get(serverId);
Create a Server (Simple)
Creates a new Server. Returns preliminary information about the Server as well as an Action that covers progress of creation.
Creating a server is pretty straightforward. We can specify the basics like name, image, location, or get more detailed, indicating whether we'll enable IPv4, IPv6, or specifying if it'll be within a private network. We can set SSH keys, decide if it should be associated with a volume, or even kick off a startup script using Cloud config.
The minimum required to create the server is the location, the image, the server name, and the server type
In this example, we'll specify the minimum to create a server.
HetznerCloudClient hetznerCloudClient =newHetznerCloudClient("apiKey");long datacenterId =4; // ID of Datacenter to create Serverlong imageId =45557056; // ID or name of the Image the Server is created fromstring name ="name-example"; // Name of the Server to create (must be unique per Project and a valid hostname as per RFC 1123)long serverTypeId =22; // ID or name of the Image the Server is created fromServer server =awaithetznerCloudClient.Server.Create(datacenterId, imageId, name, serverTypeId);
Additionally, the eDataCenter enum has been created, aiming to standardize resource creation throughout the Hetzner environment. It can be used for server creation as follows
HetznerCloudClient hetznerCloudClient =newHetznerCloudClient("ApiKey");eDataCenter eDataCenter =eDataCenter.ash; // Enum for the data center where it will be createdlong imageId =45557056; // ID or name of the Image the Server is created fromstring name ="name-example"; // Name of the Server to create (must be unique per Project and a valid hostname as per RFC 1123)long serverTypeId =22; // ID or name of the Image the Server is created fromServer server =awaithetznerCloudClient.Server.Create(eDataCenter, imageId, name, serverTypeId);
Create a Server (Complete)
In addition to creating a server (simple), we can specify every detail of the server's resources. This includes indicating whether to enable IPv4, IPv6, passing a list of network IDs, SSH key IDs, specifying the volumes to mount, and even including it in a "placement group." We can also indicate the startup script with Cloud-Init scripts
HetznerCloudClient hetznerCloudClient =newHetznerCloudClient("ApiKey");long datacenterId =4; // ID of Datacenter to create Serverlong imageId =45557056; // ID or name of the Image the Server is created fromstring name ="name-example"; // Name of the Server to create (must be unique per Project and a valid hostname as per RFC 1123)long serverTypeId =22; // ID or name of the Image the Server is created from// Optional;List<long> privateNetoworksIds =newList<long> { 3562839 }; // List<long> containing the IDs of the private networksList<long> sshKeysIds =newList<long> { 13121954,16371855 }; // List<long> containing the SSH keys that the resource will useList<long> volumesIds =newList<long> { 100090124 }; // List<long> containing the IDs of the volumes that will be attached to the server and mounted automaticallylong placementGroupId =270736; // ID or Placement GroupServer server =awaithetznerCloudClient.Server.Create( datacenterId, imageId, name, serverTypeId, ipv4:true, ipv6:true, privateNetoworksIds: privateNetoworksIds, sshKeysIds: sshKeysIds, volumesIds: volumesIds, placementGroupId: placementGroupId, userData:"#cloud-config"+"\npackages:"+"\n- cadaver"+"\n- unzip"+"\npackage_update: true"+"\npackage_upgrade: true");
And just like in the simple creation, here you can also replace LocationId with eDataCenter to make the creation process simpler
HetznerCloudClient hetznerCloudClient =newHetznerCloudClient("ApiKey");eDataCenter eDataCenter =eDataCenter.ash; // Enum for the data center where it will be createdlong imageId =45557056; // ID or name of the Image the Server is created fromstring name ="name-example"; // Name of the Server to create (must be unique per Project and a valid hostname as per RFC 1123)long serverTypeId =22; // ID or name of the Image the Server is created from// Optional;List<long> privateNetoworksIds =newList<long> { 3562839 }; // List<long> containing the IDs of the private networksList<long> sshKeysIds =newList<long> { 13121954,16371855 }; // List<long> containing the SSH keys that the resource will useList<long> volumesIds =newList<long> { 100090124 }; // List<long> containing the IDs of the volumes that will be attached to the server and mounted automaticallylong placementGroupId =270736; // ID or Placement GroupServer server =awaithetznerCloudClient.Server.Create( eDataCenter, imageId, name, serverTypeId, ipv4:true, ipv6:true, privateNetoworksIds: privateNetoworksIds, sshKeysIds: sshKeysIds, volumesIds: volumesIds, placementGroupId: placementGroupId, userData:"#cloud-config"+"\npackages:"+"\n- cadaver"+"\n- unzip"+"\npackage_update: true"+"\npackage_upgrade: true");
Update a Server
Updates a Server. You can update a Serverβs name.
HetznerCloudClient hetznerCloudClient =newHetznerCloudClient("ApiKey");// Get one serverServer server =awaithetznerCloudClient.Server.Get(39706661);// Change nameserver.Name="new-name";// Updateserver =awaithetznerCloudClient.Server.Update(server);
Note:
Person asking: Can you only modify the name in the api.hetzner.cloud/v1/servers/{id} endpoint?
Person answering: Yes!
Person asking: π
All other actions that affect or interact with the server can be found in Servers Actions.
Delete a Server
Deletes a Server. This immediately removes the Server from your account, and it is no longer accessible. Any resources attached to the server (like Volumes, Primary IPs, Floating IPs, Firewalls, Placement Groups) are detached while the server is deleted.
HetznerCloudClient hetznerCloudClient =newHetznerCloudClient("ApiKey");// Get one serverServer server =awaithetznerCloudClient.Server.Get(39707117);// DeleteawaithetznerCloudClient.Server.Delete(server);
You can also delete by passing the Server ID instead of the Server object