Flex.io Javascript SDK

    Version 1.8.3

    Basics

    Table of contents

    Installation

    Node.js

    The preferred way of installing the Flex.io SDK for Node.js is to use the npm package manager. Simply go to your project's directory and enter the following command at the command prompt:

    Source code

    The source code for the Flex.io Javascript SDK is available on GitHub. You can download or view it here.

    Setup

    You can generate an API by logging into your account on Flex.io.

    Collections

    Connections

    Pipes

    Connections

    Using custom headers

    Bearer token authentication

    Basic authentication

    OAuth2 authentication

    Pipes

    Conversion

    Dead-simple CSV to JSON conversion in two lines of code

    Execute

    Run some basic Javascript code

    Simple input/output with Javascript jobs

    Request

    GET request with no parameters

    GET request with parameters

    Request shorthand with no parameters

    Request shorthand with parameters

    Request using a connection

    POST request with data

    Render

    Render a webpage with shorthand parameters (defaults to PNG)

    Render a webpage to PDF with shorthand parameters

    Render a webpage to PNG with extended params

    Render a webpage to PDF with extended params

    Reference

    Flexio.setup()

    Flexio.setup(apiKey, [options])

    This function allows you to supply your API key to the SDK library. An API key can be generated from the dashboard on Flex.io after you log in.

    Example:

    var Flexio = require('flexio-sdk-js')
    Flexio.setup('YOUR-API-KEY')
    

    Flexio.connections.create()

    Flexio.connections.create(conn, callback(err))

    Creates a new connection object. The connection object specified in the parameter conn can either be a plain object with the desired key/value pairs set or alternativily a Flexio.connection object.

    If you wish to specify a plain object, please see the API documentation for more information on the required parameters.

    Example 1:

    var conn = {
        "name": "HTTP Connection",
        "description": "HTTP Connection to API",
        "connection_type": "http",
        "connection_info": {
            "url": "https://api.service.com/endpoint",
            "headers": {
                "Authorization": "Bearer :token",
                "Content-Type": "application/json"
            }
        }
    }
    
    Flexio.connections.create(conn, function (err, response) {
        console.log("Error (if any) was: " + err)
        console.log("Response was: " + response)
    })
    

    Example 2:

    var conn = Flexio.connection()
    conn.name("Hello")
    conn.description("New connection")
    
    Flexio.connections.create(conn, function (err, response) {
        console.log("Error (if any) was: " + err)
        console.log("Response was: " + response)
    })
    

    Flexio.connections.load()

    Flexio.connections.load(identifier, callback(err, conn))

    Loads a connection object. Once the load operation has completed, the callback function will be invoked.

    var Flexio = require('flexio-sdk-js')
    Flexio.setup('YOUR-API-KEY')
    
    Flexio.connections.load('my-connection-alias', function(err, conn) {
      // use or modify connection object `conn` here
    })
    

    Flexio.connections.list()

    Flexio.connections.list(callback)

    Retrieve a list of connections available to the user. Upon completion of the call, an array of connection info objects will be returned via the callback function.

    Example:

    var Flexio = require('flexio-sdk-js')
    Flexio.setup('YOUR-API-KEY')
    
    Flexio.connections.list(function(err, conns) {
      console.log(conns)
    })
    

    Flexio.pipes.create()

    Flexio.pipes.create(pipe, callback(err))

    Creates a new pipe. The pipe object specified in the parameter pipe can either be a plain object with the desired key/value pairs set or alternativily a Flexio.pipe object.

    If you wish to specify a plain object, please see the API documentation for more information on the required parameters.

    Example 1:

    var pipe = {
      "name": "Flexio Website",
      "ename": "my-custom-pipe-alias",
      "description": "Flex.io Website",
      "task": [
          {
              "type": "flexio.request",
              "params": {
                  "url": "https://www.flex.io"
              }
          }
      ]
    }
    
    Flexio.pipes.create(pipe, function (err, response) {
        console.log("Error (if any) was: " + err)
        console.log("Response was: " + response)
    })
    

    Example 2:

    var pipe = new Flexio.pipe()
    pipe.request('https://www.flex.io')
    
    Flexio.pipes.create(pipe, function (err, response) {
        console.log("Error (if any) was: " + err)
        console.log("Response was: " + response)
    })
    

    Flexio.pipes.load()

    Flexio.pipes.load(identifier, callback(err,pipe))

    Loads a pipe object. Once the load operation has completed, the callback function will be invoked.

    var Flexio = require('flexio-sdk-js')
    Flexio.setup('YOUR-API-KEY')
    
    Flexio.pipes.load('my-pipe-alias', function(err, pipe) {
      // use or modify, or run pipe object `pipe` here. For example:
      pipe.run(function() { console.log('Pipe ran successfully!') })
    })
    

    Flexio.pipes.list()

    Flexio.pipes.list(callback(err,pipe))

    Retrieve a list of pipes available to the user. Upon completion of the call, an array of pipes will be returned via the callback function.

    Example:

    var Flexio = require('flexio-sdk-js')
    Flexio.setup('YOUR-API-KEY')
    
    Flexio.pipes.list(function(err, pipes) {
      console.log(pipes)
    })
    

    Flexio.pipes.run()

    Flexio.pipes.run([pipeIdentifier|pipeObject|taskArray])
    Flexio.pipes.run([pipeIdentifier|pipeObject|taskArray], callback)
    Flexio.pipes.run([pipeIdentifier|pipeObject|taskArray], {params}, callback)

    Calling this method will cause a pipe to run. The pipe is specified by the first parameter, and can be a pipe identifier (such as a pipe id or pipe alias) or a pipe object created with Flexio.pipe() or retrieved by Flexio.pipes.load(). The caller can also specify an array of javascript objects specifying the raw task parameters for pipe execution.

    Additional parameters, such as query string parameters (so-called 'GET parameters'), or form parameters and files may be specified in the optional params object.

    When the pipe has finished executing, the callback function will be invoked if one has been specified. The spec for the callback function is

    function(err, response) { }
    

    The response object has several properties used to retrieve the response payload. response.text is used to get a text payload. response.data is used to get a JSON object payload. The raw binary payload, which is used for images and other binary data, can be directly accessed via response.buffer, and in an ArrayBuffer object. In a web browser client setting, a Blob object can be accessed via response.blob.

    var Flexio = require('flexio-sdk-js')
    Flexio.setup('YOUR-API-KEY')
    
    var pipe = Flexio.pipe()
                     .request('https://now.httpbin.org')
    
    Flexio.pipes.run(pipe, function(err, response) {
      console.log(response.text)
    })
    

    pipe.run()

    pipe.run()
    pipe.run(callback(err,response))
    pipe.run({params}, callback(err,response)))

    These functions are convenience functions that invoke the functionality of Flexio.pipes.run(), which is documented above.

    Example:

    var Flexio = require('flexio-sdk-js')
    Flexio.setup('YOUR-API-KEY')
    
    var pipe = Flexio.pipe()
                     .echo('Hello, World')
                     .run(function(err, response) {
                        console.log(response.text)
                     })
    
    pipe.echo()
    pipe.execute()

    pipe.javascript()

    pipe.javascript(javascriptFunction)
    pipe.javascript(javascriptCodeString)

    pipe.javascript() adds a Javascript execute step to the pipe. If a function is specified, it is automatically used as the flexio_handler function. If Javascript code is specified in a string, the code must include a callback handler which will be invoked upon execution:

    exports.flexio_handler = function(context) {
      // code to run goes here
    }
    

    Example 1:

    var Flexio = require('flexio-sdk-js')
    Flexio.setup('YOUR-API-KEY')
    
    Flexio.pipe()
      .javascript(function(context) {
        for (var i = 1; i <= 10; ++i) {
          context.output.write(''+i)
          if (i < 10) {
            context.output.write(',')
          }
        }
      })
      .run(function(err, response) {
        console.log(response.text)
      })
    

    Example 2:

    var Flexio = require('flexio-sdk-js')
    Flexio.setup('YOUR-API-KEY')
    
    Flexio.pipe()
      .javascript(`
        exports.flexio_handler = function(context) {
          context.output.write("Hello, World.")
        }
      `)
      .run(function(err, response) {
        console.log(response.text)
      })
    

    pipe.render()

    pipe.render(params)

    pipe.render() renders a web page to an image or PDF file. By default, a PNG image of the webpage is generated. An optional params argument can be specified with various options that modify the behavior of the rendering process. Here are a list of the options that can be specified on the params object:

    OptionDescription
    formatThe file format to generate (possible values are 'jpeg', 'pdf', or 'png'). Defaults to 'png'
    fullA boolean true/false indicating whether the full webpage from top to bottom should be rendered. Defaults to false.
    heightThe height of the resulting image. If full is specified, this argument is ignored. Defaults to 600
    scrollbarsA boolean true/false indicating whether or not scrollbars are included in the rendered image. Defaults to false.
    paperWhen rendering a PDF, this option specifies the paper size to use. (Possible values 'A4, 'Letter', etc.) Defaults to 'Letter'
    urlThe URL to navigate to
    widthThe width of the resulting image. If full is specified, this argument is ignored. Defaults to 800

    Example:

    var Flexio = require('flexio-sdk-js')
    Flexio.setup('YOUR-API-KEY')
    
    Flexio.pipe()
    .render({
      "url": "https://www.flex.io",
      "format": "png",
      "width": 1024,
      "height": 768,
      "scrollbars": false
    })
    .run(function(err, response) {
      // image returned as 'response.blob' or 'response.buffer' on node.js
    })
    

    pipe.python()

    pipe.python(pythonCodeString)

    pipe.python() adds a Python execute step to the pipe. The code for the script is passed as a string parameter to the function.

    Example:

    var Flexio = require('flexio-sdk-js')
    Flexio.setup('YOUR-API-KEY')
    
    Flexio.pipe()
      .python(`
    def flexio_handler(context):
        s = ",".join(str(i) for i in range(1,11))
        context.output.write(s)
      `)
      .run(function(err, response) {
        console.log(response.text)
      })