id: serverapi title: Server API Reference ---

This page describes the application programming interface (API) between the EnvayaSMS app and your server. Deploying EnvayaSMS requires implementing a script on your server in accordance with this API.

For convenience, EnvayaSMS includes server libraries and example code for certain languages.

Drupal users can also install the Drupal module (developed by Mark Burdett / VozMob).

If a server library is not yet available for your programming language, you can still use EnvayaSMS by implementing code in accordance with the API reference below. We encourage you to contribute new libraries and example code back to the EnvayaSMS project!

Contents

API Overview

EnvayaSMS communicates with the server via HTTP POST requests, called actions. These actions include:

All of these POST requests are made to the same URL (the "Server URL"), with data passed as normal POST parameters. Each POST request has an "action" parameter that tells the server which action is being performed.

For each action sent by the phone, the server sends an HTTP response (as JSON). If the action was successful, the response should contain zero or more events. Events include:

Optionally, EnvayaSMS can also connect directly to an AMQP server (such as RabbitMQ). This allows your server to push outgoing messages to the phone in real-time, instead of requiring the phone to poll for messages at a fixed interval. Each message in AMQP queue is a single event, also encoded as JSON.

(Note: Prior to version 3.0, the HTTP response format was XML. See information on upgrading to version 3.0.)

HTTP Request Format (Actions)

The following parameters are sent in all POST requests from EnvayaSMS:

"version" ::= <integer>
EnvayaSMS's version code. This is an integer that will be incremented whenever a new version of EnvayaSMS is released. (It is not the same as the version name shown on the Help screen.)

This allows the server to support phones running different API versions at the same time. If a deployment has many phones running with EnvayaSMS, the server should update its code first, then the phones can be upgraded to the new version of EnvayaSMS as convenient.
"phone_number" ::= <text>
The phone number of the phone running EnvayaSMS, as entered under Menu > Settings.

This allows the server to differentiate between EnvayaSMS clients if multiple phones are running EnvayaSMS.
"log" ::= <text>
Log messages printed to the EnvayaSMS console since the last successful HTTP request.

You may wish to append this data to a log file to allow administrators to see error details without needing physical access to the phone.

EnvayaSMS does not guarantee that the server receives log messages in order. Occasionally, the server may receive the same log message multiple times.
"network" ::= <text>
The phone's current type of internet connectivity. Typically this is something like "MOBILE" or "WIFI".
"settings_version" ::= <integer>
The current value of the "settings_version" setting. (The server may optionally set this setting by pushing settings to the phone in a 'settings' event.)

This allows the server to determine if it should push new settings to the phone, allowing you to manage and update the app settings remotely (i.e. without manually typing them into the app).
"now" ::= <long integer>
The current time in milliseconds since the Unix epoch at midnight, January 1, 1970 UTC, according to the phone's clock.
"battery" ::= <integer>
The current percentage of the phone's battery level, from 0 to 100.
"power" ::= <integer>
The current power source of the Android phone; 0=battery, 1=USB; 2=AC
"action" ::= "outgoing" | "incoming" | "send_status" | "device_status" | "test"
          | "amqp_started" | "forward_sent"
The request action determines the purpose of the HTTP request:
"outgoing":
Poll the server for events (including, but not limited to, outgoing messages)
"incoming":
Forward an incoming message to the server (SMS, MMS, or call notification)
"send_status":
Update the server on the status of sending an outgoing message
"device_status":
Notify the server of a change in the Android device's state.
"test":
Test the server connection.
"amqp_started":
The phone has established a real-time connection to an AMQP server.
"forward_sent":
Forwards a message to the server that was previously sent via the Android phone's Messaging app.
The other POST parameters sent depend on the request action.
The following HTTP Headers are sent in all POST requests from EnvayaSMS:
"X-Request-Signature" ::= <text>
A signature of the request to verify the phone and the server share the same password. (This doesn't protect against MITM snooping or replay attacks, so it is recommended to use the https:// protocol.)

The signature is calculated by the following algorithm:
  1. Sort all POST parameters, not including file uploads, by the name of the field (in the usual ASCII order).
  2. Generate an input string by concatenating:
    • the server URL,
    • each of the sorted POST parameters, in the format name=value for each name/value pair,
    • the password,
    with a comma in between each element, like so:
    "<serverURL>,<name1>=<value1>,<...>,<nameN>=<valueN>,<password>"
  3. Generate the SHA-1 hash of the input string in UTF-8
  4. Encode the SHA-1 hash using Base64 with no line breaks.
Additional parameters sent in POST requests with action=incoming:
"from" ::= <text>
The phone number of the message sender.
"message_type" ::= "sms" | "mms" | "call"
Whether this message is a SMS, MMS, or a notification of an incoming call.
"message" ::= <text>
The message body of the SMS, or the content of the text/plain part of the MMS. For multipart SMS messages, this field contains all parts concatenated and may be longer than 160 characters. (This field is empty for incoming call notifications.)
"timestamp" ::= <long integer>
The timestamp of the incoming message, in milliseconds since midnight, January 1, 1970 UTC.
Additional parameters sent in POST requests with action=incoming and message_type=mms:
"mms_parts" ::= <json_array>
Metadata for each part of the MMS. Each item in the JSON array is an object with the following keys and values:
"name" ::= <text>
The name of an additional form field where the content of the MMS part is sent as an attached file.
"cid" ::= <text>
The Content ID of the MMS part. This allows the server to resolve references in the SMIL part of the MMS (e.g. <img region="Image" src="cid:805"/>).
"type" ::= "application/smil" | "text/plain" | "image/jpeg" | ...
The Content Type of the MMS part.
"filename" ::= <text>
The filename of the MMS part, as sent by the sender phone, e.g. "Image001.jpg". In cases when the original filename is not available, this filename is a random string.
In addition, the request contains form fields with the content of each MMS part, with names as listed in the mms_parts field. Text parts are encoded in UTF-8.
Additional parameters sent in POST requests with action=outgoing:
(None)
Additional parameters sent in POST requests with action=send_status:
"id" ::= <text>
The Server's ID for the outgoing message (from the id attribute of an sms tag in a previous XML response from the server).
"status" ::= "queued" | "failed" | "cancelled" | "sent"
The current status of the outgoing message.
"error" ::= <text>
A description of the reason for the error, if the message failed to send; or, an empty string if the message has been sent successfully.
Additional parameters sent in POST requests with action=device_status:
"status" ::= "power_connected" | "power_disconnected"
             | "battery_low" | "battery_okay"
This field describes a condition that has changed on the Android device.
"power_connected":
The phone is now connected to external power.
"power_disconnected":
This phone is no longer connected to external power.
"battery_low":
The phone's battery level has dropped below 15%.
"battery_okay":
The phone's battery level is now at least 20% (after dropping below 15%).
"send_limit_exceeded":
EnvayaSMS is delaying sending outgoing messages because the rate limit for sending messages has been exceeded. (Installing additional expansion packs may be needed.)
Additional parameters sent in POST requests with action=forward_sent:
"to" ::= <text>
The phone number that the message was sent to.
"message_type" ::= "sms"
Currently only SMS messages are supported.
"message" ::= <text>
The message body of the SMS.
"timestamp" ::= <long integer>
The timestamp of the outgoing message, in milliseconds since midnight, January 1, 1970 UTC.
Additional parameters sent in POST requests with action=amqp_started:
"consumer_tag" ::= <text>
The consumer tag of the AMQP connection.

The server can use this action to kick off old AMQP connections (that weren't closed properly) before their heartbeat timeout expires. With RabbitMQ, this can be done using the management API.

HTTP Response Format (Events)

In response to a successful HTTP request (i.e., action), the server should return HTTP status code 200, with an application/json content type.

The response body should contain a JSON object with an events property, which is an array of events. Each event is a JSON object, containing one or more properties.

For example, the following response contains one 'send' event that instructs EnvayaSMS to send one message:

{
 "events":[{
   "event":"send",
   "messages":[{
     "id":"4f7c9cea5e11b",
     "to":"6507993371",
     "message":"hello world'"
   }]
 }]
}

All events contain at least the following property:

"event" ::= "send" | "cancel" | "cancel_all" | "log" | "settings"
This property determines the type of event:
"send":
Send one or more outgoing messages
"cancel":
Attempt to cancel an outgoing message that has been queued.
"cancel_all":
Attempt to cancel all outgoing messages that have been queued.
"log":
Display a message in the EnvayaSMS app log.
"settings":
Update some of EnvayaSMS's settings.
Additional properties for send events:
"messages" ::= <array>
An array of messages to send. Each message is an object with the following properties:
"id" ::= <string> (optional)
An ID for this outgoing message. (EnvayaSMS will send this back to the server as the id field in a send_status request.)
"to" ::= <string> (optional for incoming, required for outgoing)
The phone number to send the SMS to.

This may be omitted (null) if the event is sent as a response to action=incoming. In this case, the message will be sent as a reply to the original sender.
"priority" ::= <integer> (optional)
The priority level of the outgoing message. If your server is sending outgoing messages faster than the phone can send them out, the priority level allows you to specify the order to send them (e.g. so that you can send transactional SMS replies before asynchronous notifications).

Larger integers represent higher priorities. If omitted, the default priority level is 0.

Within a priority level, SMS messages are processed and sent in the order they were received from the server, and from top to bottom within the XML rsponse.
"message" ::= <string>
The content of the SMS message to send. If the content is longer than the maximum size of a single SMS (typically 160 characters), it will automatically be sent as a multipart SMS.
Additional properties for cancel events:
"id" ::= <string>
The ID of the message to cancel sending, previously sent via a "send" event.
Additional properties for cancel_all events:

(None)

Additional properties for log events:
"message" ::= <string>
A string to display in the EnvayaSMS log.
Additional properties for settings events:
"settings" ::= <object>

An object where each property name is the name of an EnvayaSMS setting, and each property's value is the new value for that setting.

The following settings are used within EnvayaSMS:

enabled ::= <boolean>
server_url ::= <string>
phone_number ::= <string>
password ::= <string>
outgoing_interval ::= <integer>
keep_in_inbox ::= <boolean>
call_notifications ::= <boolean>
forward_sent ::= <boolean>
network_failover ::= <boolean>
test_mode ::= <boolean>
auto_add_test_number ::= <boolean>
ignore_shortcodes ::= <boolean>
ignore_non_numeric ::= <boolean>
amqp_enabled ::= <boolean>
amqp_port ::= <integer>
amqp_vhost ::= <integer>
amqp_ssl ::= <boolean>
amqp_user ::= <string>
amqp_password ::= <string>
amqp_queue ::= <string>
amqp_heartbeat ::= <integer>
market_version ::= <integer>
market_version_name ::= <string>
settings_version ::= <integer>

HTTP Response Error Format

If the phone's HTTP request failed for some reason, the server should return an appropriate HTTP error code (400-499 for client errors, 500-599 for server errors).

To show a detailed error message in the EnvayaSMS app logs, set the content type as application/json and return a JSON object as the response body, like so:

{
 "error":{
  "message:"Your error message"
 }
}

AMQP Message Format

If you are using AMQP to push outgoing messages and other events to the phone in real time, the content type for each message should be application/json and the body of each message should be a serialized JSON object representing a single event, as defined in the preceding section.

Testing Your Implementation

The EnvayaSMS Request Simulator is a standalone HTML file that allows you to simulate EnvayaSMS's HTTP requests entirely in your browser via JavaScript.

Just copy the HTML file somewhere on your site, and open it in a web browser. The URL of the EnvayaSMS Request Simulator must be on the same domain as the Server URL.