Hetzner Cloud Go SDK
Overview
The official Go SDK for Hetzner Cloud provides type-safe access to 23+ resource types with automatic retries, action polling, and comprehensive error handling. Use it for bots, automation, integrations, and complex workflows. For quick CLI operations, use hetzner:hcloud-cli instead.
Quick Setup
import "github.com/hetznercloud/hcloud-go/v2/hcloud"
// Create client
client := hcloud.NewClient(hcloud.WithToken("your-api-token"))
go get github.com/hetznercloud/hcloud-go/v2/hcloud
Quick Reference
| Task |
Method |
| Servers |
|
| List servers |
client.Server.All(ctx) |
| Get server |
client.Server.GetByID(ctx, 123) or GetByName(ctx, "web") |
| Create server |
client.Server.Create(ctx, hcloud.ServerCreateOpts{}) |
| Delete server |
client.Server.Delete(ctx, server) |
| Reboot/Reset |
client.Server.Reboot(ctx, server) / Reset(ctx, server) |
| Networks |
|
| Create network |
client.Network.Create(ctx, hcloud.NetworkCreateOpts{}) |
| Attach server |
client.Server.AttachToNetwork(ctx, server, opts) |
| Volumes |
|
| Create volume |
client.Volume.Create(ctx, hcloud.VolumeCreateOpts{}) |
| Attach volume |
client.Volume.Attach(ctx, volume, server) |
| Actions |
|
| Wait for action |
client.Action.WaitFor(ctx, action) |
| Poll with callback |
client.Action.WaitForFunc(ctx, callback, action) |
API Categories
See references/api-reference.md for complete method list:
- Servers (create, lifecycle, networking)
- Networks, subnets, routes
- Volumes
- Firewalls and rules
- Load balancers, targets, services
- Floating IPs, Primary IPs
- SSH keys, images, certificates
- DNS zones (GA in v2.30.0)
- Storage boxes (experimental)
Client Configuration
client := hcloud.NewClient(
hcloud.WithToken("token"), // Required
hcloud.WithEndpoint("https://api.hetzner.cloud/v1"), // Custom endpoint
hcloud.WithApplication("myapp", "1.0.0"), // User-Agent
hcloud.WithDebugWriter(os.Stderr), // Debug logging
hcloud.WithHTTPClient(customClient), // Custom HTTP client
hcloud.WithRetryOpts(hcloud.RetryOpts{ // Retry config
MaxRetries: 5,
BackoffFunc: hcloud.ExponentialBackoff(2, time.Second),
}),
hcloud.WithPollOpts(hcloud.PollOpts{ // Action polling
BackoffFunc: hcloud.ConstantBackoff(500 * time.Millisecond),
}),
)
Common Patterns
See references/patterns.md for idiomatic patterns:
- Error handling
- Action polling
- Pagination
- Resource lookups
Action Handling
All long-running operations return an Action:
result, _, err := client.Server.Create(ctx, opts)
if err != nil {
return err
}
// Wait for completion
if err := client.Action.WaitFor(ctx, result.Action); err != nil {
return err
}
// Or with progress callback
err = client.Action.WaitForFunc(ctx,
func(update *hcloud.Action) error {
fmt.Printf("Progress: %.0f%%\n", update.Progress)
return nil
},
result.Action,
)
Error Handling
import "github.com/hetznercloud/hcloud-go/v2/hcloud"
err := someAPICall()
// Check specific error codes
if hcloud.IsError(err, hcloud.ErrorCodeNotFound) {
// Resource doesn't exist
}
// Get error details
if apiErr, ok := err.(*hcloud.APIError); ok {
fmt.Printf("Error: %s - %s\n", apiErr.Code, apiErr.Message)
}
Common error codes:
ErrorCodeNotFound - Resource doesn't exist
ErrorCodeInvalidInput - Validation error
ErrorCodeForbidden - Insufficient permissions
ErrorCodeRateLimitExceeded - Rate limit hit (auto-retried)
ErrorCodeConflict - Resource changed (auto-retried)
ErrorCodeLocked - Another action running
Common Mistakes
| Problem |
Solution |
| Nil pointer panic |
Always check error before using result |
| Action timeout |
Use ctx, cancel := context.WithTimeout(...) |
| Missing pagination |
Use client.Server.All(ctx) for complete list |
| Action failed |
Check action error with WaitFor() return value |
| Rate limiting |
SDK auto-retries, but add backoff for bulk ops |
1---2name: hcloud-go-sdk3description: Use when writing Go code to interact with Hetzner Cloud API - automation, infrastructure provisioning, bots, integrations, or programmatic cloud operations4---5
6# Hetzner Cloud Go SDK
7
8## Overview
9
10The official Go SDK for Hetzner Cloud provides type-safe access to 23+ resource types with automatic retries, action polling, and comprehensive error handling. Use it for bots, automation, integrations, and complex workflows. For quick CLI operations, use `hetzner:hcloud-cli` instead.
11
12## Quick Setup
13
14```go
15import "github.com/hetznercloud/hcloud-go/v2/hcloud"
16
17// Create client
18client := hcloud.NewClient(hcloud.WithToken("your-api-token"))
19```
20
21```bash
22go get github.com/hetznercloud/hcloud-go/v2/hcloud
23```
24
25## Quick Reference
26
27| Task | Method |
28|------|--------|
29| **Servers** | |
30| List servers | `client.Server.All(ctx)` |
31| Get server | `client.Server.GetByID(ctx, 123)` or `GetByName(ctx, "web")` |
32| Create server | `client.Server.Create(ctx, hcloud.ServerCreateOpts{})` |
33| Delete server | `client.Server.Delete(ctx, server)` |
34| Reboot/Reset | `client.Server.Reboot(ctx, server)` / `Reset(ctx, server)` |
35| **Networks** | |
36| Create network | `client.Network.Create(ctx, hcloud.NetworkCreateOpts{})` |
37| Attach server | `client.Server.AttachToNetwork(ctx, server, opts)` |
38| **Volumes** | |
39| Create volume | `client.Volume.Create(ctx, hcloud.VolumeCreateOpts{})` |
40| Attach volume | `client.Volume.Attach(ctx, volume, server)` |
41| **Actions** | |
42| Wait for action | `client.Action.WaitFor(ctx, action)` |
43| Poll with callback | `client.Action.WaitForFunc(ctx, callback, action)` |
44
45## API Categories
46
47See `references/api-reference.md` for complete method list:
48- Servers (create, lifecycle, networking)
49- Networks, subnets, routes
50- Volumes
51- Firewalls and rules
52- Load balancers, targets, services
53- Floating IPs, Primary IPs
54- SSH keys, images, certificates
55- DNS zones (GA in v2.30.0)
56- Storage boxes (experimental)
57
58## Client Configuration
59
60```go
61client := hcloud.NewClient(
62 hcloud.WithToken("token"), // Required
63 hcloud.WithEndpoint("https://api.hetzner.cloud/v1"), // Custom endpoint
64 hcloud.WithApplication("myapp", "1.0.0"), // User-Agent
65 hcloud.WithDebugWriter(os.Stderr), // Debug logging
66 hcloud.WithHTTPClient(customClient), // Custom HTTP client
67 hcloud.WithRetryOpts(hcloud.RetryOpts{ // Retry config
68 MaxRetries: 5,
69 BackoffFunc: hcloud.ExponentialBackoff(2, time.Second),
70 }),
71 hcloud.WithPollOpts(hcloud.PollOpts{ // Action polling
72 BackoffFunc: hcloud.ConstantBackoff(500 * time.Millisecond),
73 }),
74)
75```
76
77## Common Patterns
78
79See `references/patterns.md` for idiomatic patterns:
80- Error handling
81- Action polling
82- Pagination
83- Resource lookups
84
85## Action Handling
86
87All long-running operations return an `Action`:
88
89```go
90result, _, err := client.Server.Create(ctx, opts)
91if err != nil {
92 return err
93}
94
95// Wait for completion
96if err := client.Action.WaitFor(ctx, result.Action); err != nil {
97 return err
98}
99
100// Or with progress callback
101err = client.Action.WaitForFunc(ctx,
102 func(update *hcloud.Action) error {
103 fmt.Printf("Progress: %.0f%%\n", update.Progress)
104 return nil
105 },
106 result.Action,
107)
108```
109
110## Error Handling
111
112```go
113import "github.com/hetznercloud/hcloud-go/v2/hcloud"
114
115err := someAPICall()
116
117// Check specific error codes
118if hcloud.IsError(err, hcloud.ErrorCodeNotFound) {
119 // Resource doesn't exist
120}
121
122// Get error details
123if apiErr, ok := err.(*hcloud.APIError); ok {
124 fmt.Printf("Error: %s - %s\n", apiErr.Code, apiErr.Message)
125}
126```
127
128Common error codes:
129- `ErrorCodeNotFound` - Resource doesn't exist
130- `ErrorCodeInvalidInput` - Validation error
131- `ErrorCodeForbidden` - Insufficient permissions
132- `ErrorCodeRateLimitExceeded` - Rate limit hit (auto-retried)
133- `ErrorCodeConflict` - Resource changed (auto-retried)
134- `ErrorCodeLocked` - Another action running
135
136## Common Mistakes
137
138| Problem | Solution |
139|---------|----------|
140| Nil pointer panic | Always check error before using result |
141| Action timeout | Use `ctx, cancel := context.WithTimeout(...)` |
142| Missing pagination | Use `client.Server.All(ctx)` for complete list |
143| Action failed | Check action error with `WaitFor()` return value |
144| Rate limiting | SDK auto-retries, but add backoff for bulk ops |