This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| manuals:vps:api [2026/11/01 19:48] – Organize authentication and CLI usage and update installation and Ruby examples aither | manuals:vps:api [2026/11/01 19:49] (current) – Add a client overview and complete examples for PHP, Python, JavaScript, TypeScript, Go and Rust aither | ||
|---|---|---|---|
| Line 17: | Line 17: | ||
| ===== Working with the API ===== | ===== Working with the API ===== | ||
| - | The API is built on the [[https:// | + | The API uses the [[https:// |
| - | which we have developed. The framework creates | + | Its [[https:// |
| - | a [[https:// | + | lists the available resources, actions and parameters. |
| - | API, which means that premade generic clients can be | + | |
| - | used: | + | |
| - | * Ruby - https:// | + | The following |
| - | * PHP - https:// | + | |
| - | * JavaScript - https:// | + | |
| - | * Go - https:// | + | |
| - | Usage examples can always be found in the '' | + | * [[# |
| - | receives the API URL, then the client | + | * [[# |
| + | * [[# | ||
| + | * [[# | ||
| + | * [[# | ||
| + | * [[# | ||
| + | * [[# | ||
| The API is [[wp> | The API is [[wp> | ||
| Line 600: | Line 600: | ||
| end | end | ||
| </ | </ | ||
| + | |||
| + | ===== Other clients ===== | ||
| + | |||
| + | Each example requests an [[# | ||
| + | password, then prints the ID and hostname of up to 10 VPSes. Replace '' | ||
| + | and '' | ||
| + | login code with the commented alternative and fill in '' | ||
| + | |||
| + | ==== PHP client ==== | ||
| + | |||
| + | Install the [[https:// | ||
| + | using Composer: | ||
| + | |||
| + | <code bash> | ||
| + | composer require haveapi/ | ||
| + | </ | ||
| + | |||
| + | Save this example as '' | ||
| + | with '' | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | require __DIR__ . '/ | ||
| + | |||
| + | $api = new \HaveAPI\Client(' | ||
| + | $api-> | ||
| + | // Or replace the login call above with an existing token: | ||
| + | // $api-> | ||
| + | |||
| + | foreach ($api-> | ||
| + | echo $vps->id . ': ' . $vps-> | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | See the [[https:// | ||
| + | for other actions and error handling. | ||
| + | |||
| + | ==== Python client ==== | ||
| + | |||
| + | Install [[https:// | ||
| + | |||
| + | <code bash> | ||
| + | python -m pip install haveapi-client | ||
| + | </ | ||
| + | |||
| + | Save this example as '' | ||
| + | |||
| + | <code python> | ||
| + | from haveapi_client import Authenticated, | ||
| + | |||
| + | with Client(" | ||
| + | login = api.begin_token_login({ | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | }) | ||
| + | if not isinstance(login, | ||
| + | raise RuntimeError(" | ||
| + | # Or replace the login block above with an existing token: | ||
| + | # api.set_auth(TokenAuth(" | ||
| + | |||
| + | result = api.vps.list(limit=10) | ||
| + | for vps in result.data: | ||
| + | print(vps[" | ||
| + | </ | ||
| + | |||
| + | The [[https:// | ||
| + | also covers asynchronous calls with '' | ||
| + | |||
| + | ==== Dynamic HaveAPI client for JavaScript and TypeScript ==== | ||
| + | |||
| + | Both languages use the same npm package, which includes TypeScript declarations: | ||
| + | |||
| + | <code bash> | ||
| + | npm install haveapi-client | ||
| + | </ | ||
| + | |||
| + | This example works in JavaScript and TypeScript. To run it with Node.js, save it | ||
| + | as '' | ||
| + | |||
| + | <code javascript> | ||
| + | import { AsyncClient } from " | ||
| + | |||
| + | async function main() { | ||
| + | const api = new AsyncClient(" | ||
| + | await api.authenticate(" | ||
| + | // Or replace the login call above with an existing token: | ||
| + | // await api.authenticate(" | ||
| + | const result = await api.resource(" | ||
| + | params: { limit: 10 }, | ||
| + | }); | ||
| + | const vpses = result.response.data; | ||
| + | if (Array.isArray(vpses)) { | ||
| + | for (const vps of vpses) { | ||
| + | console.log(vps.id, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | main().catch(() => console.error(" | ||
| + | </ | ||
| + | |||
| + | See the [[https:// | ||
| + | for other actions and browser usage. | ||
| + | |||
| + | ==== Go client ==== | ||
| + | |||
| + | Add the [[https:// | ||
| + | to your Go module: | ||
| + | |||
| + | <code bash> | ||
| + | go get github.com/ | ||
| + | </ | ||
| + | |||
| + | Save this example as '' | ||
| + | |||
| + | <code go> | ||
| + | package main | ||
| + | |||
| + | import ( | ||
| + | " | ||
| + | " | ||
| + | |||
| + | " | ||
| + | ) | ||
| + | |||
| + | func main() { | ||
| + | api := client.New(" | ||
| + | if err := api.SetNewTokenAuth(& | ||
| + | User: | ||
| + | Password: " | ||
| + | }); err != nil { | ||
| + | log.Fatal(err) | ||
| + | } | ||
| + | // Or replace the login block above with an existing token: | ||
| + | // api.SetExistingTokenAuth(" | ||
| + | |||
| + | action := api.Vps.Index.Prepare() | ||
| + | action.NewInput().SetLimit(10) | ||
| + | response, err := action.Call() | ||
| + | if err != nil { | ||
| + | log.Fatal(err) | ||
| + | } | ||
| + | if !response.Status { | ||
| + | log.Fatalf(" | ||
| + | } | ||
| + | |||
| + | for _, vps := range response.Output { | ||
| + | fmt.Println(vps.Id, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | See the [[https:// | ||
| + | for other actions and parameters. | ||
| + | |||
| + | ==== Generated TypeScript client for vpsAdmin ==== | ||
| + | |||
| + | The [[https:// | ||
| + | provides typed methods for vpsAdmin resources and actions. Install it using npm, | ||
| + | which also installs its '' | ||
| + | |||
| + | <code bash> | ||
| + | npm install vpsadmin-typescript-client | ||
| + | </ | ||
| + | |||
| + | This example works in JavaScript and TypeScript. To run it with Node.js, save it | ||
| + | as '' | ||
| + | Use '' | ||
| + | |||
| + | <code javascript> | ||
| + | import { AsyncClient } from " | ||
| + | import { createHaveApiExecutor, | ||
| + | |||
| + | async function main() { | ||
| + | const runtime = new AsyncClient(" | ||
| + | await runtime.authenticate(" | ||
| + | // Or replace the login call above with an existing token: | ||
| + | // await runtime.authenticate(" | ||
| + | const api = createVpsAdminClient({ execute: createHaveApiExecutor(runtime) }); | ||
| + | const page = await api.vps.index({ params: { limit: 10 } }); | ||
| + | for (const vps of page.data) { | ||
| + | console.log(vps.id, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | main().catch(() => console.error(" | ||
| + | </ | ||
| + | |||
| + | See the [[https:// | ||
| + | for other actions and types. | ||
| + | |||
| + | ==== Rust client ==== | ||
| + | |||
| + | Add the [[https:// | ||
| + | Rust project. This example uses synchronous calls, so enable the '' | ||
| + | |||
| + | <code bash> | ||
| + | cargo add vpsadmin-client --features blocking | ||
| + | </ | ||
| + | |||
| + | Save this example as '' | ||
| + | |||
| + | <code rust> | ||
| + | use vpsadmin_client:: | ||
| + | |||
| + | fn main() -> Result< | ||
| + | let login = Client:: | ||
| + | let response = login.authentication__token__token().request() | ||
| + | .user(" | ||
| + | .password(" | ||
| + | .lifetime(" | ||
| + | .send()?; | ||
| + | let TokenStep:: | ||
| + | return Err(" | ||
| + | }; | ||
| + | let api = Client:: | ||
| + | .token(token.expose_secret()) | ||
| + | .build()?; | ||
| + | // Or replace the login and client setup above with an existing token: | ||
| + | // let api = Client:: | ||
| + | |||
| + | let response = api.vps().index().limit(10).send()?; | ||
| + | for vps in response.output { | ||
| + | if let (Some(id), Some(hostname)) = (vps.id, vps.hostname) { | ||
| + | println!(" | ||
| + | } | ||
| + | } | ||
| + | Ok(()) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | See the [[https:// | ||
| + | for other actions, asynchronous calls and Rust requirements. | ||