|
1 | | -# hetzner-dns-client |
| 1 | +# Node.js Hetzner DNS API Client |
| 2 | + |
| 3 | +[](https://dns.hetzner.com/api-docs/) |
| 4 | +[](https://npmjs.org/package/hetzner-dns-client) |
| 5 | +[](https://npmjs.org/package/hetzner-dns-client) |
| 6 | + |
2 | 7 | Hetzner DNS API client for Node.js |
| 8 | + |
| 9 | +## Installation |
| 10 | +```bash |
| 11 | +npm i hetzner-dns-client |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | +You can use this client library with your Node.js project. |
| 16 | + |
| 17 | +### JavaScript Example |
| 18 | +```js |
| 19 | +import {HetznerDnsClient, DnsRecord} from 'hetzner-dns-client'; |
| 20 | + |
| 21 | +// create client instance |
| 22 | +const client = new HetznerDnsClient("YOUR_API_TOKEN"); |
| 23 | +// get zone by ID |
| 24 | +const zone = await client.zones.get("ZONE_ID"); |
| 25 | +// create apex/root zone record of type "A" to 127.0.0.1 |
| 26 | +const record = await zone.createRecord("@", DnsRecord.Type.A, "127.0.0.1"); |
| 27 | +``` |
| 28 | + |
| 29 | +### TypeScript Example |
| 30 | + |
| 31 | +```ts |
| 32 | +import {HetznerDnsClient, DnsRecord, Zone} from 'hetzner-dns-client'; |
| 33 | + |
| 34 | +// create client instance |
| 35 | +const client: HetznerDnsClient = new HetznerDnsClient("YOUR_API_TOKEN"); |
| 36 | +// get zone by ID |
| 37 | +const zone: Zone = await client.zones.get("ZONE_ID"); |
| 38 | +// create apex/root zone record of type "A" to 127.0.0.1 |
| 39 | +const record: DnsRecord = await zone.createRecord("@", DnsRecord.Type.A, "127.0.0.1"); |
| 40 | +``` |
| 41 | + |
| 42 | +### Handling Errors |
| 43 | +Methods that may throw an error are marked with `throws` in the documentation and specify what error types may be thrown. You can catch these errors with a `try/catch` block. |
| 44 | + |
| 45 | +For example: |
| 46 | + |
| 47 | +```js |
| 48 | +try { |
| 49 | + const zone = await client.zones.get("ID that does not exist"); |
| 50 | +} |
| 51 | +catch (err) { |
| 52 | + if (err instanceof ApiError) |
| 53 | + console.error(err); // the Hetzner API returned an error, e.g. "Zone not found" |
| 54 | + else if (err instanceof ClientParseError) |
| 55 | + console.error(err); // the client could not understand or parse the response from the Hetzner API |
| 56 | + else |
| 57 | + console.error(err); // some other error occurred |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## API Documentation |
| 62 | +The official Hetzner DNS API documentation can be found on [https://dns.hetzner.com/api-docs/](https://dns.hetzner.com/api-docs/). |
| 63 | + |
| 64 | +> **Note**: The Hetzner DNS API documentation appears to be incomplete and contains a few errors. The documentation of this client library is a separate documentation and some functionality may be described differently. |
| 65 | +> |
| 66 | +> If you find any errors in the documentation, or think that further clarification is needed, please open an issue or pull request. |
| 67 | +
|
| 68 | +The client library documentation is available in DOCS.md in this repository. |
0 commit comments