Create a Site via API
Creating a new site using APIs is simple but you need to know a couple of things.
When you create a new site, it may have 2 states:
Status = ready
Status = in progress
In the case of "in progress", you can query our task status API to see when the site will be ready. Lets understand this with examples:
- Create Site (from Scratch)
- Create Site (from Template) - Contains examples for both Shared & Private templates.
To create a site using APIs, you can call this endpoint:
You will get something like this in the output:
You can extract:
URL: https://xyzsitename.instawp.xyz
Username: zuvalebuca7835
Password: iUnzmX2l1WRk3uMQA0NS
Autologin Hash: $2y$10$xviWFKNaIREgtkLWQw6Jnurgk8ePIT9AGyWvS8DoFscqQP4BfqAmu
Check for is_pool = true or false which will tell you that the site is ready or you need to wait for it to be ready. If is_pool is false, you will also get a task_id which can be used to query the state of the site creation.
To query our status API , send a request like this:
Response:
You can query the task status end point every X seconds till you have a complete site.
Private Templates are templates saved from a websites in order for re-use (in other words Blueprints). These templates are private, meaning they can be used by you or your team only, they are not accessible by guest users.
To create a site using APIs, you can call this endpoint:
template_slug can be found in your template details page or via template list API. (coming soon.. )
site_name is optional, without this parameter it will create a site with auto generated name.
is_reserved is optional, without this parameter OR setting it as false will create a Temporary site.
If you mark a template as shared, any guest user (even without InstaWP account) can create website. However, to use it via API you will need to use the API token.
Note that we are supplying is_shared parameter as true in this case.
You can also password email to simulate the site creation with a guest's email.
💡 Did you know - You can auto-create new WordPress instances when creating a new Pull Request (PR) on your GitHub repository. Learn about Github actions.
When you create a new site, it may have 2 states:
Status = ready
Status = in progress
In the case of "in progress", you can query our task status API to see when the site will be ready. Lets understand this with examples:
- Create Site (from Scratch)
- Create Site (from Template) - Contains examples for both Shared & Private templates.
Create Site (from Scratch)
To create a site using APIs, you can call this endpoint:
<?php
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'https://app.instawp.io/api/v2/sites', [
'body' => '{"site_name":"xyzsitename"}',
'headers' => [
'authorization' => 'Bearer <api_key>',
'content-type' => 'application/json',
],
]);
echo $response->getBody();
You will get something like this in the output:
{
"status": true,
"message": "Site installation work is completed, Your website is ready!",
"data": {
"message": "Site installation work is completed, Your website is ready!",
"is_pool": true,
"status": 1,
"wp_url": "https://xyzsitename.instawp.xyz",
"wp_username": "zuvalebuca7835",
"wp_password": "iUnzmX2l1WRk3uMQA0NS",
"id": 1333819,
"s_hash": "$2y$10$xviWFKNaIREgtkLWQw6Jnurgk8ePIT9AGyWvS8DoFscqQP4BfqAmu",
"token": null,
"remaining_site_minutes": 1439,
"install_content_task_id": false,
"dispatch_migrate_v3": false
}
}
You can extract:
URL: https://xyzsitename.instawp.xyz
Username: zuvalebuca7835
Password: iUnzmX2l1WRk3uMQA0NS
Autologin Hash: $2y$10$xviWFKNaIREgtkLWQw6Jnurgk8ePIT9AGyWvS8DoFscqQP4BfqAmu
Check for is_pool = true or false which will tell you that the site is ready or you need to wait for it to be ready. If is_pool is false, you will also get a task_id which can be used to query the state of the site creation.
Check Progress
To query our status API , send a request like this:
$response = $client->request('GET', "https://app.instawp.io/api/v2/tasks/d53a490af3cd/status", [
'headers' => [
'authorization' => 'Bearer <token>',
],
]);
Response:
{
"status":true,
"message":"Task details retrived.",
"data":{
"id":148601,
"team_id":1565,
"user_id":1504,
"type":"site_install",
"comment":null,
"cloud_task_id":"f42764a58ddd",
"resource_id":98716,
"resource_type":"App\\Models\\Site",
"success_callback_fun":"successICSiteInstall",
"error_callback_fun":"failedICSiteInstall",
"percentage_complete":"0.00",
"status":"progress",
"timeout_at":"2022-12-14 11:25:43",
"task_meta":null,
"created_at":"2022-12-14T11:23:43.000000Z",
"updated_at":"2022-12-14T11:23:43.000000Z"
}
}
You can query the task status end point every X seconds till you have a complete site.
Complete code
<?php
require_once "vendor/autoload.php";
$client = new \GuzzleHttp\Client();
$token = "your token";
$response = $client->request("POST", "https://app.instawp.io/api/v2/sites", [
"body" => '{"site_name":"xyzsitename1"}',
"headers" => [
"authorization" => "Bearer $token",
"content-type" => "application/json",
],
]);
//echo $response->getBody();
$site = json_decode($response->getBody());
// check if site is from pool, then no need to proceed.
$task_id = $site->data->task_id;
while (true) {
echo "[.] Waiting for the site to be created...\n";
$response = $client->request(
"GET",
"https://app.instawp.io/api/v2/tasks/$task_id/status",
[
"headers" => [
"authorization" =>
"Bearer $token",
],
]
);
$task = json_decode($response->getBody());
if ($task->data->status != "progress") {
echo "[+] Site Created : {$site->data->wp_url}\n";
break;
}
sleep(2);
}
Create Website (from Template)
Private Templates
Private Templates are templates saved from a websites in order for re-use (in other words Blueprints). These templates are private, meaning they can be used by you or your team only, they are not accessible by guest users.
To create a site using APIs, you can call this endpoint:
<?php
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'https://app.instawp.io/api/v2/sites/template', [
'body' => '{
"template_slug": "slug",
"site_name": "mysite",
"is_reserved" : true
}',
'headers' => [
'authorization' => 'Bearer <api_key>',
'content-type' => 'application/json',
],
]);
echo $response->getBody();
template_slug can be found in your template details page or via template list API. (coming soon.. )
site_name is optional, without this parameter it will create a site with auto generated name.
is_reserved is optional, without this parameter OR setting it as false will create a Temporary site.
Shared Template
If you mark a template as shared, any guest user (even without InstaWP account) can create website. However, to use it via API you will need to use the API token.
<?php
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'https://app.instawp.io/api/v2/sites/template', [
'body' => '{
"template_slug": "slug",
"site_name": "mysite",
"is_reserved" : true,
"is_shared": true,
}',
'headers' => [
'authorization' => 'Bearer <api_key>',
'content-type' => 'application/json',
],
]);
echo $response->getBody();
Note that we are supplying is_shared parameter as true in this case.
You can also password email to simulate the site creation with a guest's email.
💡 Did you know - You can auto-create new WordPress instances when creating a new Pull Request (PR) on your GitHub repository. Learn about Github actions.
Updated on: 14/11/2024
Thank you!