Skip to content

Commit f18c9a6

Browse files
authored
docs: load token from env in examples scripts (#302)
1 parent 6d46d06 commit f18c9a6

4 files changed

Lines changed: 33 additions & 7 deletions

File tree

examples/create_server.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
from __future__ import annotations
22

3+
from os import environ
4+
35
from hcloud import Client
46
from hcloud.images import Image
57
from hcloud.server_types import ServerType
68

7-
# Please paste your API token here between the quotes
8-
client = Client(token="{YOUR_API_TOKEN}")
9+
assert (
10+
"HCLOUD_TOKEN" in environ
11+
), "Please export your API token in the HCLOUD_TOKEN environment variable"
12+
token = environ["HCLOUD_TOKEN"]
13+
14+
client = Client(token=token)
15+
916
response = client.servers.create(
1017
name="my-server",
1118
server_type=ServerType("cx11"),

examples/list_servers.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
from __future__ import annotations
22

3+
from os import environ
4+
35
from hcloud import Client
46

5-
client = Client(
6-
token="{YOUR_API_TOKEN}"
7-
) # Please paste your API token here between the quotes
7+
assert (
8+
"HCLOUD_TOKEN" in environ
9+
), "Please export your API token in the HCLOUD_TOKEN environment variable"
10+
token = environ["HCLOUD_TOKEN"]
11+
12+
client = Client(token=token)
813
servers = client.servers.get_all()
914
print(servers)

examples/usage_oop.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
from __future__ import annotations
22

3+
from os import environ
4+
35
from hcloud import Client
46
from hcloud.images import Image
57
from hcloud.server_types import ServerType
68

9+
assert (
10+
"HCLOUD_TOKEN" in environ
11+
), "Please export your API token in the HCLOUD_TOKEN environment variable"
12+
token = environ["HCLOUD_TOKEN"]
13+
714
# Create a client
8-
client = Client(token="project-token")
15+
client = Client(token=token)
916

1017
# Create 2 servers
1118
# Create 2 servers

examples/usage_procedurale.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
from __future__ import annotations
22

3+
from os import environ
4+
35
from hcloud import Client
46
from hcloud.images import Image
57
from hcloud.server_types import ServerType
68
from hcloud.servers import Server
79
from hcloud.volumes import Volume
810

9-
client = Client(token="project-token")
11+
assert (
12+
"HCLOUD_TOKEN" in environ
13+
), "Please export your API token in the HCLOUD_TOKEN environment variable"
14+
token = environ["HCLOUD_TOKEN"]
15+
16+
client = Client(token=token)
1017

1118
# Create 2 servers
1219
response1 = client.servers.create(

0 commit comments

Comments
 (0)