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.
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.
wp_username and wp_password are empty when the site is still installing
The response always contains wp_username and wp_password keys, but they are only filled in when the site came from the pool (is_pool: true, "Site installation work is completed").
When you get a task_id instead ("Site installation in progress"), both come back as empty strings by design — the WordPress admin user does not exist yet at that point. Don't treat this as an error and don't retry the create call. Wait for the task to finish, then fetch the credentials from the Get Site Credentials endpoint below.
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.
Get Site Credentials
Once the task status is no longer progress, the WordPress admin user exists and you can read the credentials. They are not returned by the site list or site detail endpoints — use the dedicated credentials endpoint, passing the site id you got back as data.id from the create call:
$response = $client->request('GET', "https://app.instawp.io/api/v2/sites/$site_id/credentials", [
'headers' => [
'authorization' => 'Bearer <api_key>',
],
]);Response:
{
"status": true,
"message": "Site credentials retrieved.",
"data": {
"wp_username": "zuvalebuca7835",
"wp_password": "iUnzmX2l1WRk3uMQA0NS",
"wp_admin_email": "you@example.com",
"server_username": "xyzsitename",
"server_password": "cJ8yQ2m0PdVt",
"ip_addr": "203.0.113.10"
}
}wp_username/wp_password— the WordPress admin login for<site>/wp-admin.server_username/server_password/ip_addr— the SFTP/SSH login for the site's files.- Your API token needs the
viewability on that 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());
$site_id = $site->data->id;
// If the site came from the pool it is already installed and the create response
// carries the credentials — nothing left to wait for.
if (!empty($site->data->is_pool)) {
echo "[+] Site Created : {$site->data->wp_url}\n";
echo "[+] Username : {$site->data->wp_username}\n";
echo "[+] Password : {$site->data->wp_password}\n";
exit(0);
}
// Otherwise the site is still installing: wp_username / wp_password in the
// response above are empty strings, and we poll the task until it finishes.
$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);
}
// The WordPress admin user now exists — fetch the credentials.
$response = $client->request(
"GET",
"https://app.instawp.io/api/v2/sites/$site_id/credentials",
[
"headers" => [
"authorization" => "Bearer $token",
],
]
);
$creds = json_decode($response->getBody());
echo "[+] Username : {$creds->data->wp_username}\n";
echo "[+] Password : {$creds->data->wp_password}\n";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_slugcan be found in your template details page or via template list API. (coming soon.. )site_nameis optional, without this parameter it will create a site with auto generated name.is_reservedis 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_sharedparameter as true in this case. - You can also password
emailto 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.
Related Articles
- InstaWP API - API authentication, methods and parameters
- Link Two Sites Programmatically - Connect two sites over the API
- GitHub Actions - Auto-create sites on a GitHub PR
- Create an API Token - Generate the token you'll authenticate with