@modexvpn/hcloud
Version:
SDK for Hetzner Cloud API
220 lines (168 loc) ⢠4.91 kB
Markdown
# @modexvpn/hcloud
A clean, type-safe SDK for the Hetzner Cloud API using Node.js, TypeScript, and Zod.
## Features
- š **Zod-powered runtime validation** ā fully JavaScript-safe
- š§ **Fully type-safe** using TypeScript with `z.infer<>` and enums
- š Modular structure: `hcloud.servers.list()`, `hcloud.locations.getById()`
- ā ļø Centralized error handling with `formatHcloudError()`
- āļø Axios-based HTTP client with auto-authentication
- š ESM and CommonJS compatible
- šļø Clean folder structure for scalability and maintainability
## SDK Status
This SDK is **stable**, but not yet feature-complete.
We're already on **v1.x** and will maintain backward compatibility.
Any breaking changes will be released as **v2.0.0**.
---
## š What's New in v1.4
- `createSSHKey(options)` ā Create a new SSH key
- `getSSHKeys()` ā List all SSH keys
- `getSSHKey(id)` ā Get a single SSH key by ID
- `updateSSHKey(id, options)` ā Update an existing SSH key
- `deleteSSHKey(id)` ā Delete an SSH key
### āļø Usage Examples
#### Create SSH Key
```ts
const sshKey = await hcloud.security.createSSHKey({
name: "modex-prod-key",
public_key: "ssh-ed25519 AAAAC3N...",
labels: { environment: "prod" }
})
```
#### List SSH Keys
```ts
const allKeys = await hcloud.security.listSSHKeys()
```
#### Get SSH Key by ID
```ts
const key = await hcloud.security.getSSHKey(12345)
```
#### Update SSH Key
```ts
const updated = await hcloud.security.updateSSHKey(12345, {
name: "modex-updated-key",
labels: { updated: "true" }
})
```
#### Delete SSH Key
```ts
await hcloud.security.deleteSSHKey(12345)
```
---
## š Installation
```bash
npm install @modexvpn/hcloud
```
---
## š§ Usage
### List all servers
```ts
import { hcloud } from '@modexvpn/hcloud'
const servers = await hcloud.servers.list()
```
### Get a single server by ID
```ts
const server = await hcloud.servers.getById(123456)
```
### Fetch server metrics
```ts
const metrics = await hcloud.servers.getMetrics(123456)
```
### Power control
```ts
await hcloud.servers.powerControl.off(123456)
await hcloud.servers.powerControl.reboot(123456)
await hcloud.servers.powerControl.on(123456)
await hcloud.servers.powerControl.reset(123456)
await hcloud.servers.powerControl.shutdown(123456)
```
### Create a server
```ts
await hcloud.servers.create({
name: 'test-server',
server_type: 'cx21',
image: 'ubuntu-22.04',
location: 'nbg1',
ssh_keys: ['ssh-key-id'],
})
```
### Delete a server
```ts
await hcloud.servers.delete(123456)
```
### SSH Into Server
```ts
const { stdout } = await sshIntoServer("192.168.1.100", "apt update && apt upgrade -y", "root")
console.log(stdout)
```
> š” Requires `SSH_PRIVATE_KEY` in your environment
---
## š¢ Environment Setup
Create a `.env` file and add your Hetzner API token:
```env
HCLOUD_API_TOKEN=your-token-here
```
---
## š Project Structure
```
@modexvpn/hcloud
āāā client.ts
āāā index.ts
āāā sdk/
ā āāā servers/
ā ā āāā actions/
ā ā ā āāā sshIntoServer.ts
ā ā āāā createServer.ts
ā ā āāā server-type/
ā ā āāā getServerTypes.ts
ā ā āāā getServerType.ts
ā ā āāā index.ts
ā āāā locations/
ā āāā datacenters/
ā āāā getDatacenters.ts
ā āāā getDatacenter.ts
āāā types/
ā āāā common/
ā āāā enums/
ā āāā errors/
ā āāā ...
āāā utils/
ā āāā formatError.ts
āāā tsconfig.json
āāā tsup.config.ts
āāā README.md
```
---
## š« API Coverage
### Core Resources
- [x] [Errors](https://docs.hetzner.cloud/#errors)
- [x] [Servers](https://docs.hetzner.cloud/#servers)
- [~] [Actions](https://docs.hetzner.cloud/#server-actions)
- [x] [Types](https://docs.hetzner.cloud/#server-types)
- [x] [Locations](https://docs.hetzner.cloud/#locations)
- [x] [Datacenters](https://docs.hetzner.cloud/#datacenters)
- [ ] [Actions](https://docs.hetzner.cloud/#actions)
### Security
- [x] [SSH Keys](https://docs.hetzner.cloud/#ssh-keys)
- [ ] [Certificates](https://docs.hetzner.cloud/#certificates)
### Storage & Networking
- [ ] [Volumes](https://docs.hetzner.cloud/#volumes)
- [ ] [Networks](https://docs.hetzner.cloud/#network)
### Firewalls & IPs
- [ ] [Firewalls](https://docs.hetzner.cloud/#firewalls)
- [ ] [Floating IPs](https://docs.hetzner.cloud/#floating-ips)
### Load Balancers
- [ ] [Load Balancers](https://docs.hetzner.cloud/#load-balancers)
### Billing
- [ ] [Pricing](https://docs.hetzner.cloud/#pricing)
---
## š Contributing
PRs welcome! Please follow the existing structure and run:
```bash
npm run lint
npm run build
```
---
## š Resources
- [Hetzner Cloud API Docs](https://docs.hetzner.cloud)
## š License
MIT Ā© 2025 MODEX