Servers are virtual machines that can be provisioned.
Get all Servers
Returns all existing Server objects
HetznerCloudClient hetznerCloudClient = new HetznerCloudClient("apiKey");
// Get All
List<Server> list = await hetznerCloudClient.Server.Get();
Get a Server
Returns a specific Server object. The Server must exist inside the Project
HetznerCloudClient hetznerCloudClient = new HetznerCloudClient("apiKey");
long serverId = 39447794;
// Get One
Server server = await hetznerCloudClient.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 = new HetznerCloudClient("apiKey");
long datacenterId = 4; // ID of Datacenter to create Server
long imageId = 45557056; // ID or name of the Image the Server is created from
string 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
Server server = await hetznerCloudClient.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 = new HetznerCloudClient("ApiKey");
eDataCenter eDataCenter = eDataCenter.ash; // Enum for the data center where it will be created
long imageId = 45557056; // ID or name of the Image the Server is created from
string 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
Server server = await hetznerCloudClient.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 = new HetznerCloudClient("ApiKey");
long datacenterId = 4; // ID of Datacenter to create Server
long imageId = 45557056; // ID or name of the Image the Server is created from
string 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 = new List<long> { 3562839 }; // List<long> containing the IDs of the private networks
List<long> sshKeysIds = new List<long> { 13121954, 16371855 }; // List<long> containing the SSH keys that the resource will use
List<long> volumesIds = new List<long> { 100090124 }; // List<long> containing the IDs of the volumes that will be attached to the server and mounted automatically
long placementGroupId = 270736; // ID or Placement Group
Server server = await hetznerCloudClient.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 = new HetznerCloudClient("ApiKey");
eDataCenter eDataCenter = eDataCenter.ash; // Enum for the data center where it will be created
long imageId = 45557056; // ID or name of the Image the Server is created from
string 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 = new List<long> { 3562839 }; // List<long> containing the IDs of the private networks
List<long> sshKeysIds = new List<long> { 13121954, 16371855 }; // List<long> containing the SSH keys that the resource will use
List<long> volumesIds = new List<long> { 100090124 }; // List<long> containing the IDs of the volumes that will be attached to the server and mounted automatically
long placementGroupId = 270736; // ID or Placement Group
Server server = await hetznerCloudClient.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 = new HetznerCloudClient("ApiKey");
// Get one server
Server server = await hetznerCloudClient.Server.Get(39706661);
// Change name
server.Name = "new-name";
// Update
server = await hetznerCloudClient.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 = new HetznerCloudClient("ApiKey");
// Get one server
Server server = await hetznerCloudClient.Server.Get(39707117);
// Delete
await hetznerCloudClient.Server.Delete(server);
You can also delete by passing the Server ID instead of the Server object
HetznerCloudClient hetznerCloudClient = new HetznerCloudClient("ApiKey");
// Delete
await hetznerCloudClient.Server.Delete(39707117);