External Flex Services

As well as running in the FlexService Runtime, FlexServices can be deployed on any PaaS, local server, or hosting provider.

This guide covers the creation of FlexServices as well as running FlexServices outside of Kinvey.

Prerequisites

  1. Git for working with Kinvey repositories
  2. NodeJS v6.x and NPM (node package manager). We recommend installing NodeJS and NPM using NVM.

Creating a FlexService

The Kinvey Flex SDK emits events within your node service when Collection data is requested from your Kinvey app. These events are paired with user-defined handlers that fetch and transform data (from any number of sources) into Kinvey collections. The Flex SDK also includes modules for interacting with other Kinvey APIs (such as push notifications, email, collections access, and more).

Use the Flex SDK to write your FlexService. For more information on how to use it and API documentation, see the FlexServices Guide.

When initializing the SDK for external use, you can specify a host and port to listen on by passing an options object with an optional host and port. If no host/port is specified, localhost:10001 will be used:

sdk.service({ host: 'somehost', port: 7777 }, (err, flex) => {
  // code goes here
});

Provisioning Your FlexService

Before you can deploy your local NodeJS service to the FlexService Runtime you must provision an Internal Flex Service in the Kinvey Console and configure the Kinvey CLI to target this service.

Provisioning an External Flex Service

  1. Log into the Kinvey Console and select an environment
  2. Select the 'Service Catalog' tab
  3. Click 'Add a Service'
  4. Select 'External' under 'Flex' (on the right-hand side)
  5. Complete the required Service fields (name, description, url, and a secret key of your choice)
  6. Click 'Save Service'
  7. The newly-created Internal Flex Service should be visible in your Service Catalog

Running your FlexService

To run your FlexService, execute node . in the root of your project. Routes are:

For FlexData:

GET     /:serviceObject/                // Get all entities in a Service Object
GET     /:serviceObject/:id             // Get a single entity by id
GET     /:serviceObject/?{query}        // Get entities by query
POST    /:serviceObject/                // Create an entity
PUT     /:serviceObject/:id             // Update a service object entity
DELETE  /:serviceObject/                // Delete all service objects
DELETE  /:serviceObject/:id             // Delete a single entity by id
DELETE  /:serviceObject/?{query}        // Delete a service object by query
GET     /:serviceObject/_count          // Get a count of all records in the service object
GET     /:serviceObject/_count/?{query} // Get the count of a query result

For FlexFunctions:

POST    /_flexFunctions/:handlerName    // Execute the handler function
Got a question?