Skip to main content

SparkFun Phant IO

Latest Version: 1.0.0

The Phant class wraps the PhantIO/ API. It is a very simple data store to get up and running with.

You can view the library’s source code on GitHub. Click here to see information on the available versions of this library.

To include this library in your project, add

#require "Phant.class.nut:1.0.0"

at the top of your agent code

Class Usage

Constructor: Phant(publicKey, privateKey[, baseURL])

The Phant constructor takes three parameters: your Phant public key and private key, and an optional URL. If no URL is specified, the class will use Sparkfun’s as the base.

#require "Phant.class.nut:1.0.0"

stream <- Phant(PUBLIC_KEY, PRIVATE_KEY);

Current Limitations

We are currently recommending that you do not use the default URL in your constructor. Sparkfun is limiting traffic connecting via HTTPS more aggressively than traffic connecting via HTTP. If you are sending data to, please set your constructor’s baseURL to "".

Class Methods

Optional Callbacks

All methods in the Phant class take an optional callback function parameter. The callback expects two parameters: error and data. If the request was successful, error will be null, and data will contain the response. If the request was unsucessful, error will contain the error information.

When the optional callback is passed, the requests will be made asynchronously. If a callback is not supplied, the request will be made synchronously, and a table containing err and data will be returned.

push(data[, callback])

The push() method pushes a new event into your Phant instance. The first parameter, data, must be a table containing the data to be pushed. For example:

Device Code

function poll() {
    imp.wakeup(15, poll);
    agent.send("temp", getTemp());


Agent Code

device.on("temp", function (tempData) {
    // Asyncronous Push
    stream.push({ temp = tempData }, function(error, data) {
        if (error != null) {
            // If it failed, log the reason


The get() method retreives the current contents of your Phant instance.

stream.get(function(error, data) {
    if (err != null) {

    // Calculate average temperature
    local avgTemp = 0.0;
    foreach (datapoint in data) {
        avgTemp += datapoint.temp;

    avgTemp = avgTemp / data.len();

    // Log average temperature
    server.log("Average Temperature: " + avgTemp);


The clear() method clears all information stored in the Phant instance.

stream.clear(function(error, data) {
    if (error != null) {

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release


The Phant library and example code are licensed under the MIT License.