Examples

Experiments have been deprecated and Flags are the only entity in the system

  • The system is backwards compatible, but there are some critical issues to note until a new version of the Restful API is released:

    • Create an Experiment - creates a corresponding Flag, ignoring Experiment name.

    • Get Experiment - works as expected, ignoring Experiment name.

    • Patch Experiment - works as expected, ignoring Experiment name.

    • Get Experiments - returns only Flags that have at least one release rule configured.

    • Delete Experiment - removes a flag’s configuration and/or impression data, but does NOT delete the flag.

The following examples show how to use the REST API as well as highlight the benefits of deploying it in your environment.

Create a simple experiment

The following code can be used to create an experiment for flag default.followingView that will return true for all our users:

var request = require("request");

var options = { method: 'PUT',
  url: 'https://x-api.rollout.io/public-api/5990c4a1eae09726fa0d6041/production/experiments',
  body: JSON.stringify({
    "type": "experiment",
    "name": "following view",
    "flag": "default.followingView",
    "value": true
}),
  headers:
   { authorization: 'Bearer XXXX-XXXX-XXXX-XXXX-XXXXX',
     'content-type': 'application/json' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

The result experiment audience looks like this:

Experiment Audience page

And it’s setting like this:

Experiment Settings page

Create an experiment with property matching

The following code will create an experiment that is opened for users with email bob@example.com, please notice that this will override existing experiment for flag default.followingView it if already exists for production environment.

var request = require("request");

var options = { method: 'PUT',
  url: 'https://x-api.rollout.io/public-api/5990c4a1eae09726fa0d6041/production/experiments',
  body: JSON.stringify({
    "type": "experiment",
    "name": "following view",
    "flag": "default.followingView",
    "conditions": [
        {
            "property": {
                "name": "email",
                "operator": "in-array",
                "operand": [
                    "bob@example.com"
                ]
            },
            "value": true
        }
    ],
    "value": false
}),
  headers:
   { authorization: 'Bearer XXXXX-XXX-XXXX-XXXX-XXXXXX',
     'content-type': 'application/json' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

The result experiment audience looks like this:

Experiment Audience page

Add value to property matching

In this example we will modify an existing experiment:

Adding value to property matching Audience page

We can use following code the get the experiment value:

var request = require("request");

var options = { method: 'GET',
  url: 'https://x-api.rollout.io/public-api/5990c4a1eae09726fa0d6041/production/experiments/default.followingView',
  headers:
   { authorization: 'Bearer XXXXX-XXXX-XXXX-XXXX-XXXXXXX',
     'content-type': 'application/json' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

The result json looks like this

{
   "value" : false,
   "conditions" : [
      {
         "value" : true,
         "property" : {
            "operand" : [
               "bob@example.com"
            ],
            "name" : "email",
            "operator" : "in-array"
         }
      }
   ],
   "name" : "following view",
   "flag" : "default.followingView",
   "type" : "experiment"
}

Now we want to add dylan@example.com to the emails who get the value true, this will be done with the PATCH command:

var request = require("request");

var options = { method: 'PATCH',
  url: 'https://x-api.rollout.io/public-api/5990c4a1eae09726fa0d6041/production/experiments/default.followingView',
  body: JSON.stringify([{ path: "/conditions/0/property/operand/0", op:"add", value:"dylan@example.com" }]),
  headers:
   { authorization: 'Bearer XXXXX-XXXX-XXXX-XXXX-XXXXXXX',
     'content-type': 'application/json' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

If we query the experiment again, we can see dylan@example.com:

{
   "type" : "experiment",
   "value" : false,
   "name" : "following view",
   "conditions" : [
      {
         "value" : true,
         "property" : {
            "operand" : [
               "dylan@example.com",
               "bob@example.com"
            ],
            "name" : "email",
            "operator" : "in-array"
         }
      }
   ],
   "flag" : "default.followingView"
}

Remove Write permission from a specific environment

The Following code demonstrate how you can remove write permissions from a specific application’s environment using the PATCH command:

#!/bin/bash

token=<put token>
email=<put email>
app_id=<put id>
environment_name=<put env name>

encoded_email=$( echo $email | sed 's/@/%40/g') #simple email (url) encoder

curl --request PATCH \
  --url https://x-api.rollout.io/public-api/users/$encoded_email \
  --header "authorization: Bearer $token" \
  --header "content-type: application/json" \
  --data "[{\"op\":\"remove\",\"path\":\"/applications/$app_id/environments/$environment_name\",\"value\":{}}]"
  ----