1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! `HetznerClient` — credentials, transport, and the API version pin.
//!
//! **WHY:** the generated `*_request` functions take an HTTP client and a bearer
//! token per call. Something has to own both, resolve the token from the
//! environment the way the rest of the ecosystem spells it, and keep it out of
//! everything that prints.
//!
//! **WHAT:** three constructors (spec-56 decision 02 §5) — [`HetznerClient::from_env`]
//! for the ergonomic path, [`HetznerClient::new`] for callers with their own
//! secret store, and [`HetznerClient::with_client`] to inject the transport, which
//! is what the mock-API tests use.
//!
//! **HOW:** the token never leaves this module except through
//! [`HetznerClient::token`]. `Debug` is hand-written and redacts it (decision 02
//! §4): a derived `Debug` on a struct holding a bearer token is one
//! `tracing::debug!` away from putting it in a log.
use ;
use crateHetznerError;
/// The environment variables Hetzner Cloud's ecosystem uses, in precedence order.
///
/// Both are **Hetzner Cloud API** tokens — verified against each tool's source
/// (2026-07-17), because guessing here means picking up a credential for the
/// wrong product:
///
/// | Variable | Read by | Endpoint it authenticates against |
/// |---|---|---|
/// | `HCLOUD_TOKEN` | the official `hcloud` CLI, the Terraform/OpenTofu provider | `api.hetzner.cloud` |
/// | `HETZNER_API_TOKEN` | `lego` (the ACME library behind Traefik and much of cert-manager) | `api.hetzner.cloud/v1` |
///
/// `HCLOUD_TOKEN` is first because it is Hetzner's own tooling: `hcloud` derives
/// it as `"HCLOUD_" + ToUpper(option name)` where the option is `token`, and the
/// Terraform provider declares `EnvDefaultFunc("HCLOUD_TOKEN", nil)`.
///
/// **Two Hetzner variables we deliberately do not read:**
///
/// - `HETZNER_API_KEY` / `HETZNER_TOKEN` — the **DNS** API (`dns.hetzner.com`),
/// a different product with different tokens. lego marks its DNS-API path
/// deprecated in favour of the Cloud API for exactly this reason. Reading one
/// would send a DNS token to the Cloud API, earn a 401, and have us report
/// "check your token" when the truth is "that token is for another product".
/// - `TF_VAR_hcloud_token` — that is **Terraform's** namespace, not Hetzner's:
/// `TF_VAR_x` sets whatever input variable a config declared as `variable "x"`.
/// The suffix is the config author's choice, so honouring this one spelling
/// would serve one popular style and miss `variable "token"` entirely.
///
/// The override for each is **derived**, not invented: `EWE_` + the name. Strip
/// the prefix and you are back at the vendor's variable, so there is no third
/// spelling to remember (decision 02 §1).
pub const VENDOR_VARS: & = &;
/// Hetzner's API version, fixed into the client.
///
/// An API version is not a per-call decision, so callers never pass it
/// (feature 00 §3).
pub const API_VERSION: &str = "v1";
/// The base URL for Hetzner Cloud's API.
pub const DEFAULT_BASE_URL: &str = "https://api.hetzner.cloud/v1";
/// Every variable [`HetznerClient::from_env`] reads, in precedence order.
///
/// **All `EWE_*` before all vendor-native** (decision 02 §1): an override says
/// "use *this* token for this workspace", and that beats any vendor variable
/// lying around in the environment — not merely its own twin.
/// Hetzner Cloud API client.
///
/// Holds a bearer token. See the module docs on why `Debug` is hand-written.