Tenant API

The API sits on top of the Tenant Dashboard and gives you programatic access to deployment and management functions.

The Weavy API is available from version 3.1.0 of the Tenant SDK.

Recommendations

There are a number of cases were we encourage you to leverage the API:

  • Were the sheer number of tenants makes it impractical and time consuming to work in the Dashboard.
  • There is a need to programatically deploy tenants in an on-demand and time critical fashion.

API Anatomy

The Tenant API is based on REST and returns JSON-encoded responses.

  • Only available over HTTPS.
  • Utilizes HTTP error codes and methods.
  • Complex data should be sent as a JSON string.

A number of API endpoints will initate long running jobs, for example creating a tenant or backing up a database. The HTTP code of the response will indicate success or failure, and also return a reference to the actual job.

Query the API to get updated information about the progress of the job. Optionally, you can supply a callback url which will get called once the job finishes.

Authentication

In order to make authorized calls to the Tenant API, your application must first obtain an access token. Once you have an access token, you can issue authorized requests to the API.

Requests must use Bearer Authentication and the Access Token you recieve from /identity/connect/token must always be sent in the Authorization header.

Access tokens should be cached and reused until they expire.

Request an access token

The example below shows how to request an access token in C#.
Refer to the API Reference for more information.

// send data in body
var data = new List<KeyValuePair<string, string>>() {
    new KeyValuePair("grant_type", "client_credentials"),
    new KeyValuePair("scope", "api"),
    new KeyValuePair("client_id", "tenant-api"),
    new KeyValuePair("client_secret", "<your-client-secret>")
};

var response = await httpClient.PostAsync(
    "https://tenanturl.com/identity/connect/token", 
    new FormUrlEncodedContent(data)
);

// issue the request and read the response
string result = await response.Content.ReadAsStringAsync();

// parse the JSON response and get the access token
JObject jsonResult = JObject.Parse(result);
string token = jsonResult.GetValue("access_token").ToString();

Builds

Below you can find some code examples for the Builds API.

Refer to the API Documentation for the complete list of available operations.

List Builds

GET /api/builds

string token = ""; // retrive the access token
var client = new HttpClient();
client.SetBearerToken(token);
client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.GetAsync("/api/builds");

if (response.IsSuccessStatusCode) {
    var json = await response.Content.ReadAsStringAsync();
    // process the result...
} else {
    // handle error
}

Upload a build

POST /api/builds

// the build to upload
var file = new FileInfo(@"c:\\builds\\build-version.zip");
byte[] bytes = File.ReadAllBytes(file.FullName);
                    
string token = ""; // retrive the access token
var client = new HttpClient();
client.SetBearerToken(token);

// upload using multipart/form-data MIME type
using (var content = new MultipartFormDataContent("Upload----" + 
    DateTime.Now.ToString(CultureInfo.InvariantCulture))) {
        content.Add(new StreamContent(new MemoryStream(bytes)), 
        "build-version.zip", "build-version.zip");

    using (var response = await client.PostAsync("/api/builds", content)) {
        if (response.IsSuccessStatusCode) {
            var json = await response.Content.ReadAsStringAsync();
            // process the result...
        } else {
            // handle error
        }    
    }
}

Tenants

Below you can find some code examples for the Tenants API.

Refer to the API Documentation for the complete list of available operations.

Search Tenants

GET /api/tenants

string token = ""; // retrive the access token
var client = new HttpClient();
client.SetBearerToken(token);
client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json"));

// limit the list to 50 tenants
var response = await client.GetAsync("/api/tenants?top=50");

if (response.IsSuccessStatusCode) {
    var json = await response.Content.ReadAsStringAsync();
    // process the result...
} else {
    // handle error
}

Create Tenant

POST /api/tenants

string token = ""; // retrive the access token

// tenant configuration
var payload = new {
    Name = "MyTenant",
    ThemeColor = "#FF00FF",
    BuildName = "build-version.zip",
    Commment = "This tenant was created using the API!",
    CustomSettings = new Dictionary() { 
                    { "key1", "value1" }, 
                    { "key2", "value2" } 
    }
};

var client = new HttpClient();
client.SetBearerToken(token);
client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json"));
    var response = await client.PostAsync(url, 
        new StringContent(payload, Encoding.UTF8, "application/json"));

if (response.IsSuccessStatusCode) {
    var result = await response.Content.ReadAsStringAsync()    
                    
    // parse the JSON response and get the id
    JObject jsonResult = JObject.Parse(result);

    // you can use the id to query the jobs api about the status of the job
    string id = jsonResult.GetValue("Id").ToString();
} else {
    // handle error
}

Delete Tenant

POST /api/tenants/{id}/delete

string token = ""; // retrive the access token

// options to be sent with the request
var body = new { Backup = true, 
                 CallbackUrl = "https://call-when-done.com" };

var client = new HttpClient();
client.SetBearerToken(token);
client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));

var response = await client.PostAsync("/api/tenants/232/delete", 
                    new StringContent(JsonConvert.SerializeObject(body), 
                                      Encoding.UTF8, 
                                      "application/json"));