This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| manuals:vps:api [2017/02/12 15:28] – Use shortcutVpsFree::Client.new 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 ====== | ||
| - | Our API is running at https:// | + | Our API is running at https:// |
| - | actions, just like you can do from the web interface. | + | any action that you can do in the web interface. In fact, the web interface |
| - | are the management of user profiles (address, email, etc.) and data transfers. | + | running at https:// |
| - | + | action. | |
| - | In reality, the web interface running at https:// | + | |
| - | the API and calls it for every action. | + | |
| ===== 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 18: | 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 clients are available: |
| - | * PHP - https:// | + | |
| - | * JavaScript - 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 39: | Line 37: | ||
| ===== Authentication===== | ===== Authentication===== | ||
| - | There are two authentication methods | + | There are two authentication methods: |
| - | basic. The name and password must be sent along with every API request. This is a good | + | |
| - | choice for one-off actions. However, if you need to call the API several times or | + | |
| - | automatically, | + | |
| - | Another method is authentication using tokens. The way this method works is that the client first | + | * [[# |
| - | requests a token to be created, one for which the client needs the name and password. As soon as the client receives | + | * [[# |
| - | the token, the name and password can be forgotten since it is the token that is then used for further authentication. | + | |
| - | There can be several types of tokens with different lifetimes: | + | Example usage in different programming languages |
| - | * fixed - token validity is fixed | + | ==== HTTP Basic ==== |
| - | * renewable_manual - token validity can be manually extended | + | The name and password must be sent along with every API request |
| - | * renewable_auto - token validity is extended after every request | + | This is a good choice for one-off actions. However, if you need to call the API repeatedly or |
| - | * permament | + | automatically, storing the password on the disk or entering |
| + | HTTP Basic cannot be used if two-factor authentication | ||
| - | The type of token and time period by which its validity is extended is chosen by the client. | + | ==== Tokens ==== |
| + | The client first requests a token using your credentials | ||
| + | As soon as the client | ||
| + | is used for authentication. | ||
| - | ===== CLI ===== | + | There can be several types of tokens |
| - | [[https:// | + | |
| - | In order for it to work properly, you need Ruby >= 2.0 and Ruby header files, | + | |
| - | OpenSSL and ncurses (mostly packages | + | |
| - | < | + | * //fixed// - token validity is fixed |
| - | If you’re using OS X, you first need to install OpenSSL using [[http://brew.sh/|Homebrew]] and only then | + | * // |
| - | can you install EventMachine (a gem required by the client). | + | * // |
| + | * // | ||
| - | < | + | The type of token and time period by which its validity is extended is chosen |
| - | $ brew install openssl | + | by the client. |
| - | $ sudo gem install eventmachine -- --with-opt-include="/ | + | |
| - | </ | + | |
| - | </ | + | |
| - | It can be installed using ruby gems: | + | 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 ===== |
| - | $ gem install vpsfree-client | + | Authentication tokens can be restricted to selected actions. Each action has its own scope, |
| - | </ | + | you can find the available scopes in the documentation. You can configure scopes when you' |
| + | requesting a new authentication token. | ||
| - | ==== Installing in Windows 10 Using a Ubuntu Linux Subsystem ==== | + | Example scopes: |
| - | === Installing Ubuntu (Windows 10 Subsystem | + | * '' |
| + | * an empty scope will allow access only to '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| - | - in Windows 10, enable Developer Mode, let it install | + | Scopes can contain globs for resource |
| - | - in the Programs and Features menu, open Turn Windows Features On or Off, scroll to the very bottom, choose Windows Subsystem | + | |
| - | - after the restart, run bash in the Start menu as admin | + | |
| - | - create | + | |
| - | - press the Y key and let the Ubuntu base be installed | + | |
| - | === Installing | + | ===== Installing |
| + | The [[https:// | ||
| + | the '' | ||
| + | You need Ruby, Ruby header files and ncurses (usually packages with the '' | ||
| + | or '' | ||
| + | for dependency requirements. | ||
| <code bash> | <code bash> | ||
| - | sudo apt-get | + | gem install |
| </ | </ | ||
| - | === A Quick & Dirty Fix to Set Ruby2.0 as Default Instead of 1.9 === | + | Platform-specific instructions: |
| - | + | ||
| - | <code bash> | + | |
| - | sudo rm / | + | |
| - | sudo ln -s / | + | |
| - | sudo ln -s / | + | |
| - | sudo ln -s / | + | |
| - | sudo ln -s / | + | |
| - | sudo ln -s / | + | |
| - | sudo gem update --system | + | |
| - | sudo gem pristine --all | + | |
| - | </ | + | |
| - | + | ||
| - | Source: http:// | + | |
| - | + | ||
| - | === Installing vpsfree-client === | + | |
| - | + | ||
| - | <code bash> | + | |
| - | sudo gem install vpsfree-client | + | |
| - | </ | + | |
| - | + | ||
| - | + | ||
| - | ---- | + | |
| + | * [[manuals: | ||
| + | * [[manuals: | ||
| - | After the installation, | + | After the installation, |
| this can be fixed easily: | this can be fixed easily: | ||
| Line 130: | Line 113: | ||
| < | < | ||
| - | $ PATH=" | + | $ export |
| </ | </ | ||
| - | ==== Usage ==== | + | ===== Using CLI ===== |
| < | < | ||
| Line 165: | Line 148: | ||
| Commands: | Commands: | ||
| - | vps remote_console VPS_ID | + | action_state wait <STATE ID> |
| + | vps remote_console VPS_ID | ||
| + | vps migrate_many VPS_ID... | ||
| + | snapshot download [SNAPSHOT_ID] | ||
| + | snapshot send SNAPSHOT_ID | ||
| + | backup dataset [DATASET_ID] FILESYSTEM Backup dataset locally | ||
| + | backup vps [VPS_ID] FILESYSTEM | ||
| + | ip_traffic top Live IP traffic monitor | ||
| Available resources: | Available resources: | ||
| - | auth_token | ||
| cluster | cluster | ||
| cluster_resource | cluster_resource | ||
| + | cluster_resource_package | ||
| + | cluster_resource_package.item | ||
| dataset | dataset | ||
| dataset.snapshot | dataset.snapshot | ||
| dataset.plan | dataset.plan | ||
| + | dataset.property_history | ||
| dataset_plan | dataset_plan | ||
| dns_resolver | dns_resolver | ||
| Line 179: | Line 171: | ||
| environment.config_chain | environment.config_chain | ||
| environment.dataset_plan | environment.dataset_plan | ||
| + | host_ip_address | ||
| integrity_check | integrity_check | ||
| integrity_fact | integrity_fact | ||
| integrity_object | integrity_object | ||
| ip_address | ip_address | ||
| + | ip_traffic | ||
| + | ip_traffic_monitor | ||
| + | language | ||
| location | location | ||
| mail_log | mail_log | ||
| Line 188: | Line 184: | ||
| mail_template | mail_template | ||
| mail_template.recipient | mail_template.recipient | ||
| + | mail_template.translation | ||
| + | migration_plan | ||
| + | migration_plan.vps_migration | ||
| + | network | ||
| + | network_interface | ||
| node | node | ||
| + | node.status | ||
| + | object_history | ||
| os_template | os_template | ||
| pool | pool | ||
| + | session_token | ||
| snapshot_download | snapshot_download | ||
| + | system_config | ||
| + | transaction | ||
| transaction_chain | transaction_chain | ||
| - | transaction_chain.transaction | ||
| user | user | ||
| user.environment_config | user.environment_config | ||
| user.cluster_resource | user.cluster_resource | ||
| + | user.public_key | ||
| + | user.mail_role_recipient | ||
| + | user.mail_template_recipient | ||
| user.state_log | user.state_log | ||
| + | user_cluster_resource_package | ||
| + | user_cluster_resource_package.item | ||
| + | user_namespace | ||
| + | user_namespace_map | ||
| + | user_namespace_map.entry | ||
| user_session | user_session | ||
| vps | vps | ||
| Line 203: | Line 216: | ||
| vps.config | vps.config | ||
| vps.feature | vps.feature | ||
| - | vps.ip_address | ||
| vps.mount | vps.mount | ||
| + | vps.outage_window | ||
| vps.console_token | vps.console_token | ||
| + | vps.status | ||
| vps_config | vps_config | ||
| + | monitored_event | ||
| + | monitored_event.log | ||
| + | outage | ||
| + | outage.entity | ||
| + | outage.handler | ||
| + | outage_update | ||
| + | user_outage | ||
| + | vps_outage | ||
| + | vps_outage_mount | ||
| + | help_box | ||
| + | news_log | ||
| + | incoming_payment | ||
| + | payment_stats | ||
| + | user_account | ||
| + | user_payment | ||
| + | user_request | ||
| + | user_request.registration | ||
| + | user_request.change | ||
| + | action_state | ||
| </ | </ | ||
| Line 234: | Line 267: | ||
| -a, --auth METHOD | -a, --auth METHOD | ||
| -s, --save | -s, --save | ||
| - | --username | + | --user USER User name |
| --password PASSWORD | --password PASSWORD | ||
| + | --scope [SCOPE] | ||
| --token TOKEN Token | --token TOKEN Token | ||
| --token-lifetime LIFETIME | --token-lifetime LIFETIME | ||
| Line 280: | Line 314: | ||
| access to '' | access to '' | ||
| </ | </ | ||
| + | |||
| + | Specify a scope: | ||
| + | |||
| + | < | ||
| + | $ vpsfreectl --auth token --scope ' | ||
| + | </ | ||
| + | |||
| + | The command above will ask for an authentication token that will only be allowed to call actions on the vps resource and only on VPS with ID 123. | ||
| ==== Actions and Parameters ==== | ==== Actions and Parameters ==== | ||
| Line 455: | 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 540: | Line 532: | ||
| </ | </ | ||
| - | ==== Daily Downloads of VPS Backups | + | === Daily Downloads of VPS Backups === |
| < | < | ||
| # | # | ||
| Line 547: | 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 571: | Line 566: | ||
| </ | </ | ||
| - | ==== Daily NAS Backups with a Weekly History | + | === Daily NAS Backups with a Weekly History === |
| < | < | ||
| # | # | ||
| Line 578: | 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 602: | 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. | ||
| + | |||