WebMail Lite Documentation

Using Web API

Web API reference

Introduction

This API allows applications to communicate with backend code of WebMail Lite. You can create your custom frontend which would interact with WebMail Lite backend via Web API. Note that the application which uses Web API doesn't have to be written in JavaScript, it doesn't even have to be a web application, you can use this API in mobile, desktop and server applications as well.

Note that you can also use PHP API to integrate WebMail Lite with your application, assuming both are installed on the same server.

Getting started

Below, please find an example of the code written in Javascript or C#, which performs a typical Web API request.

javascript

$.ajax({
    url: 'index.php?/Api/',
    type: 'POST',
    async: true,
    dataType: 'json',
    data: {
        Module: 'Core',
        Method: 'GetSettings'
    },
    success: function (oResponse) {
        if (oResponse)
        {
            var oSettings = oResponse.Result;
            console.log('oSettings', oSettings);
        }
    }
});

C#

string url = "https://lite.afterlogic.com/?/Api/";
using (var client = new WebClient())
{
    client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    string strData = "Module=Core&Method=GetSettings";
    strData = client.UploadString(url, strData);
    Console.WriteLine(strData);
}

The following points are important in this regard:

  1. The entry point for making all the Web API calls is WebMail Lite installation URL with /?/Api/ appended.
  1. All the Web API calls are to be performed using POST;
  1. Installation contains a number of modules, therefore each request is processed by a specific module specified in POST data. Name of the method in this module is specified there as well.

Authentication

Some methods will only return useful data if you authenticate to an account. That can be done by sending Login request to Core module:

javascript

$.ajax({
    url: 'index.php?/Api/',
    type: 'POST',
    async: true,
    dataType: 'json',
    data: {
        Module: 'Core',
        Method: 'Login',
        Parameters: '{"Login":"login_value","Password":"pass_value"}'
    },
    success: function (oResponse) {
        if (oResponse && oResponse.Result)
        {
            var sAuthToken = oResponse.Result.AuthToken;
            console.log('sAuthToken', sAuthToken);
        }
    }
});

C#

string url = "https://lite.afterlogic.com/?/Api/";
using (var client = new WebClient())
{
        client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        string strData = "Module=Core&Method=Login&Parameters={Login:\"login_value\",Password:\"password_value\"}";
        strData = client.UploadString(url, strData);
        Console.WriteLine(strData);
}

CURL

curl -d "Module=Core&Method=Login&Parameters={\"Login\":\"user@domain.com\",\"Password\":\"MyPassWord\"}" -H "Content-Type: application/x-www-form-urlencoded" -X POST https://lite.afterlogic.com/?/Api/

Successful request will result in JSON-formatted response of the following kind:

{
    Module: 'Core',
    Method: 'Login',
    Result: {
        AuthToken: 'token_value'
    }
}

CORS setup

If your application and WebMail Lite are in different domains, web browser is likely to disallow sending requests between the two, due to same-origin policy.

To allow receiving requests from different domains, WebMail Lite installation needs to be sending Access-Control-Allow-Origin response header.

The simplest approach assumes allowing requests from any domain. For Apache, it's done by adding the following line to configuration file:

Access-Control-Allow-Origin: *

For Nginx webserver, it's done as follows:

add_header Access-Control-Allow-Origin *;

It can also be done for other web servers such as IIS as shown at: CORS Enabled

Error codes

The following ErrorCode values are supported:

  • 101 - invalid token;
  • 102 - authentication failure;
  • 103 - invalid data;
  • 104 - database error;
  • 113 - module not found;
  • 114 - method not found;
  • 999 - unknown error.

Web API reference