Buying a VPS through the API

API & DocumentationWhat You Can Automate·· 118 views· 5 min read

In short: you buy a VPS through the API with POST /v1/services/purchase: the plan, the billing cycle and, if you like, the system and other options. It is paid from your account credit only, at the same price you would see in My MozOut, and with the Reseller Program it already carries your Agence discount. Cloud servers are bought the same way. The terms are on the server reseller page.

The key you need

For Permission
Reading plans, options and prices catalog:read
Reading your credit billing:read
Buying from credit billing:purchase (sensitive, off by default)
Write access to the servers area, which the purchase also needs vps:write

Give this key a spending limit that fits what you usually buy. It is the most it can spend in 30 days, and a purchase over it is refused before anything is charged. Keys are created on the API page in My MozOut, and creating an API key walks you through it.

From plan to server, step by step

1 Find the plan. GET /v1/catalog/products lists what is on sale; you can filter with group_id. kind tells you what each product is and purchasable whether it can be bought through the API.
2 Open the plan with GET /v1/catalog/products/{id}. You get the cycles that have a price, the options (such as system and panel) and your price in price.
3 Check your credit with GET /v1/billing/credit.
4 Buy with POST /v1/services/purchase: product_id, billing_cycle and, optionally, config_options. The Idempotency-Key header is required: build it from your client's order number. If the connection drops, retry with the same value and you will not buy twice.
5 Keep the response. It has service_id, invoice_id and amount, the total including tax. Once paid, the server opens just as an order placed in My MozOut would: follow it with GET /v1/services/{id}.

Here is the request, with the example numbers from the documentation (plan 51, system option 51 and choice 239). Yours come from the catalog:

curl https://api.mozout.com/v1/services/purchase -X POST \
-H "Authorization: Bearer $MOZOUT_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: order-2051-vps" \
-d '{"product_id": 51, "billing_cycle": "monthly", "config_options": {"51": 239}}'

To know when the server is ready without asking over and over, save a webhook with PUT /v1/webhooks (needs webhooks:write) and pick the service.created event: it arrives when the service is created on the server, with service_id, name and domain.

The answers you will come across

Answer When What to do
402 insufficient_credit Not enough credit. Nothing was charged. Top up your credit and try again.
403 spending_limit_exceeded The purchase goes over the key's spending limit. Nothing was charged. Use a key with a higher limit.
409 profile_incomplete The account profile is not complete. Complete it in My MozOut.
422 incompatible_options A combination that does not exist, such as Windows with a cPanel licence. Pick another option.
422 promo_not_applicable The promo code does not apply to this purchase. Nothing was charged. Buy without it, or with another one.
429 rate_limited You went over 5 purchases per minute. Wait the seconds in Retry-After.
If the invoice does not match the price, the purchase is undone and nothing is charged. For a dedicated server, if purchasable is false, not_purchasable_reason explains why and the answer includes a link to buy it in My MozOut. With a sandbox_ key nothing is created: use it to test your program. See Sandbox or Production.

After the purchase

To keep track of whose server is whose, tag it with your client's name using PUT /v1/reseller/customers/service/{id} (needs reseller:write):

curl https://api.mozout.com/v1/reseller/customers/service/1470 -X PUT \
-H "Authorization: Bearer $MOZOUT_API_KEY" \
-H "Content-Type: application/json" \
-d '{"name": "João Fernandes"}'

Two things for later. A VPS renewal cycle cannot be changed through the API: it answers 409 vps_not_supported, because the amount may include add-ons. A bigger plan, on the other hand, is requested with POST /v1/services/{id}/upgrade, also paid from credit. What you can do on the server next is in controlling a VPS through the API.

Ready to buy servers for your clients at a discount? See the server reseller page and apply.

See server reselling

SEE ALSO

Catalogue and extras through the API: showing prices on your site and buying SSL or SiteLock

Sensitive API permissions and the spending limit: paying, buying, reinstalling, cPanel access

Idempotency-Key in our API: repeating a purchase, payment or ticket without doing it twice

Long-running API operations: the 202 answer, the action_id and following the result

The API for resellers and agencies: account, reseller programs and what the API does not cover

API Sandbox or Production: what each key really does, and moving your program to production

RECOMMENDED PRODUCT

VPS server with root access

Resources of your own, the OS you choose, reinstall whenever you like. from R151.20/mo (3-year plan, with coupon)

See plans
Was this answer helpful? Your answer helps us improve this article.