This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| manuals:vps:api [2024/01/02 21:05] – Split installation instructions 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 1: | Line 1: | ||
| + | < | ||
| ====== API ====== | ====== API ====== | ||
| Line 8: | Line 9: | ||
| ===== API Documentation ===== | ===== API Documentation ===== | ||
| The API documentation, | The API documentation, | ||
| - | parameters can be found at https:// | + | parameters can be found at https:// |
| Even without logging in, you can see a list of all objects, i.e. even those that only | Even without logging in, you can see a list of all objects, i.e. even those that only | ||
| Line 16: | 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:// | + | |
| - | * The HaveAPI web interface - https:// | + | |
| - | * A file system based on FUSE - 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 43: | Line 42: | ||
| * [[# | * [[# | ||
| - | Example usage in different programming languages can be found in [[https:// | + | Example usage in different programming languages can be found in [[https:// |
| ==== HTTP Basic ==== | ==== HTTP Basic ==== | ||
| Line 61: | Line 60: | ||
| * // | * // | ||
| * // | * // | ||
| - | * //permament// - the token is valid permanently, | + | * //permanent// - the token is valid permanently, |
| The type of token and time period by which its validity is extended is chosen | The type of token and time period by which its validity is extended is chosen | ||
| by the client. | by the client. | ||
| + | |||
| + | Token authentication must be enabled for your account. The examples below show | ||
| + | login with username and password. If your account also requires a TOTP code or | ||
| + | a password change, use the client' | ||
| ===== Scopes ===== | ===== Scopes ===== | ||
| Line 84: | Line 87: | ||
| ===== Installing vpsfree-client ===== | ===== Installing vpsfree-client ===== | ||
| - | [[https:// | + | The [[https:// |
| - | In order for it to work properly, you need Ruby >= 2.7, Ruby header files and ncurses (mostly | + | the '' |
| + | You need Ruby, Ruby header files and ncurses (usually | ||
| + | or '' | ||
| + | for dependency requirements. | ||
| <code bash> | <code bash> | ||
| Line 107: | Line 113: | ||
| < | < | ||
| - | $ PATH=" | + | $ export |
| </ | </ | ||
| - | ==== Using CLI ==== | + | ===== Using CLI ===== |
| < | < | ||
| Line 491: | Line 497: | ||
| </ | </ | ||
| - | ===== File system | + | ===== Ruby client |
| - | '' | + | |
| - | the API as a file system from userspace. Objects, actions and their parameters can be | + | |
| - | browsed like folders and files in any program. The file system is installed the same way | + | |
| - | as a CLI, it just has a different name: | + | |
| - | < | + | Use the library from the same '' |
| - | $ gem install haveapi-fs | + | The example requests an [[# |
| - | </code> | + | password, then lists your VPSes. Replace '' |
| + | credentials. To use an existing token, replace the login code with the commented | ||
| + | alternative and fill in '' | ||
| - | ==== Usage ==== | + | <code ruby> |
| - | The usage of '' | + | require |
| - | [[https:// | + | |
| - | brief description. | + | |
| - | < | + | api = VpsFree:: |
| - | $ haveapi-fs https://api.vpsfree.cz /mnt/api.vpsfree.cz | + | api.authenticate(: |
| - | Username: mylogin | + | lifetime: ' |
| - | Password: | + | # Or replace the login call above with an existing token: |
| + | # api.authenticate(: | ||
| - | $ cd /mnt/api.vpsfree.cz | + | api.vps.list.each do |vps| |
| - | $ ls -1 | + | puts "#{vps.id}: #{vps.hostname}" |
| - | auth_token | + | end |
| - | cluster | + | |
| - | cluster_resource | + | |
| - | dataset | + | |
| - | dataset_plan | + | |
| - | dns_resolver | + | |
| - | environment | + | |
| - | help.html | + | |
| - | help.man | + | |
| - | help.md | + | |
| - | help.txt | + | |
| - | ip_address | + | |
| - | language | + | |
| - | location | + | |
| - | mail_template | + | |
| - | migration_plan | + | |
| - | node | + | |
| - | object_history | + | |
| - | os_template | + | |
| - | snapshot_download | + | |
| - | transaction | + | |
| - | transaction_chain | + | |
| - | user | + | |
| - | user_session | + | |
| - | vps | + | |
| - | vps_config | + | |
| </ | </ | ||
| - | Each folder contains | + | See the [[https:// |
| - | the current folder contains. | + | for more library examples. |
| - | + | ||
| - | < | + | |
| - | $ cat vps/5685/hostname | + | |
| - | my-vps | + | |
| - | + | ||
| - | $ echo better-hostname > vps/ | + | |
| - | $ echo 1 > vps/ | + | |
| - | $ cat vps/ | + | |
| - | 1 | + | |
| - | $ cat vps/ | + | |
| - | better-hostname | + | |
| - | </ | + | |
| - | + | ||
| - | The example demonstrates that individual object attributes can simply be changed by | + | |
| - | writing into a file. The save action can be called by writing a one, or it can | + | |
| - | be run as an executable file. | + | |
| - | + | ||
| - | In order to get clearer creation/ | + | |
| - | '' | + | |
| - | a hash and after saving and closing the file, the corresponding action is performed. | + | |
| - | + | ||
| - | This way, you can perform any operation in the API. | + | |
| - | ===== Usage Examples | + | ==== Usage Examples ==== |
| The following scripts demonstrate ways you can use the API. These scripts can be | The following scripts demonstrate ways you can use the API. These scripts can be | ||
| run from anywhere – manually, from cron, etc. | run from anywhere – manually, from cron, etc. | ||
| Line 576: | Line 532: | ||
| </ | </ | ||
| - | ==== Daily Downloads of VPS Backups | + | === Daily Downloads of VPS Backups === |
| < | < | ||
| # | # | ||
| Line 583: | Line 539: | ||
| api = VpsFree:: | api = VpsFree:: | ||
| - | api.authenticate(: | + | api.authenticate(: |
| + | | ||
| + | # Or replace the login call above with an existing token: | ||
| + | # api.authenticate(: | ||
| api.dataset.list(role: | api.dataset.list(role: | ||
| Line 607: | Line 566: | ||
| </ | </ | ||
| - | ==== Daily NAS Backups with a Weekly History | + | === Daily NAS Backups with a Weekly History === |
| < | < | ||
| # | # | ||
| Line 614: | Line 573: | ||
| api = VpsFree:: | api = VpsFree:: | ||
| - | api.authenticate(: | + | api.authenticate(: |
| + | | ||
| + | # Or replace the login call above with an existing token: | ||
| + | # api.authenticate(: | ||
| api.dataset.list(role: | api.dataset.list(role: | ||
| Line 638: | 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. | ||
| + | |||