Articles on: API Docs

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 progress, you may need to 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 in progress, Wait for installation.",
   "data":{
      "message":"Site installation work in progress, Wait for installation.",
      "task_id":"d53a490af3cd",
      "status":1,
      "wp_url":"https:\/\/xyzsitename.instawp.xyz",
      "wp_username":"yuwehamebo0993",
      "wp_password":"q0ynLPB8jZMU3rG6DOl9",
      "id":98705,
      "s_hash":"$2y$10$movlRe2FG0R1agav\/prPF.YCCdpYjdoKphSUeF.js.zS3U4Dc0nYK",
      "token":null
   }
}


You can extract:

URL: "https://xyzsitename.instawp.xyz"
Username: yuwehamebo0993
Password: q0ynLPB8jZMU3rG6DOl9
Autologin Hash: $2y$10$movlRe2FG0R1agav\/prPF.YCCdpYjdoKphSUeF.js.zS3U4Dc0nYK

Check Progress



Also notice the task_id d53a490af3cd, which can now be used to get the status of the site installation progress.

$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());

$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: 10/04/2024

Was this article helpful?

Share your feedback

Cancel

Thank you!