How Do I Create Code Endpoints?

No comments

How Do I Create Code Endpoints?

Earlier today, we announced our newest and most powerful feature to date: code endpoints!

When you make an API call to a code endpoint, a block of user-defined code is run and returns a result. Code endpoints make it possible to create API endpoints with sophisticated functionality, without the necessary but time-consuming API legwork like security, rate-limiting, scalability, and server administration.

To create a code endpoint, all you need to do is enter source code directly into Apitite using your web browser and hit Save. That's it! No build system, no deploying, and no spinning up servers. And as usual, we take care of the security, rate-limiting, scalability,  and server administration of your API for you. Node.js is the first programming language we support for code endpoints. Eventually we will release support for additional languages like Python, Java, and .NET.

The apitite Object

To make things easy, we provide an object called apitite for performing endpoint actions like returning a result or getting a request parameter, as well as providing convenience methods for calling other existing Apitite endpoints. Here are all the methods in the apitite object: 

// Returns the given result to the API endpoint caller.

// Retrieves the value of the request parameter with the given name.

apitite.get(apiSlashEndpoint, params, callback)
// Convenience method for performing a GET request on an Apitite endpoint., params, callback)
// Convenience method for performing a POST request on an Apitite endpoint.

apitite.put(apiSlashEndpoint, params, callback)
// Convenience method for performing a PUT request on an Apitite endpoint.

apitite.delete(apiSlashEndpoint, params, callback)
// Convenience method for performing a DELETE request on an Apitite endpoint.

All callbacks have the typical Node.js signature of
    (err, result) -> undefined
If the apitite method failed for some reason, err will be an Error object with a message property explaining the error. If the apitite method succeeded, err will be null and the result of the operation will be stored as the second argument. The "-> undefined" part of the signature simply means that the callbacks you provide should not return a value. If any of this seems strange to you, please read up on how Node.js works -- it is a non-blocking, asynchronous implementation of JavaScript based mostly on callbacks.

Returning Data

To return data when your code endpoint is finished executing, you simply call apitite.done. Here is an example of calling apitite.done and passing a string: 

     apitite.done("the return value!"); // returning a string

You can pass any type of data to apitite.done, for example :

           apitite.done(1336);       // you can return a number
           apitite.done(false);      // or a boolean
           apitite.done(null);       // or null
           apitite.done(['foo', 'bar', baz']); // or an array
           apitite.done({            // or an object!
              success: true,
              result: "mo data, mo problems"

Calling Existing APIs

A powerful feature of code endpoints is that you can easily call existing Apitite endpoints, making it easy to combine results of multiple endpoints to create powerful API mashups. To make a request to an existing Apitite endpoint simply use the apitite.getapitite.postapitite.put, or apitite.delete methods, and pass it the target endpoint's unique API and endpoint path without a leading slash. Here is an example of how to call an existing Apitite endpoint with the apitite utility class.

           apitite.get('sales-api/sales-endpoint', {
               startDate : "2015-01-01",
               endDate   : "2015-07-01",
           }, function(err, salesData) {
               if (err)
                 apitite.done('Error: ' + err.message);

NOTE: All Apitite GET requests made in this manner return a JSON object, so you do not need to append /json to the end of the path, as you would if you were making a request in your own application.

Off-the-Shelf Functionality With npm Packages

npm is an amazing repository with tons of extremely useful software packages (libraries) that you can call from your code endpoint. By default, we've installed the most popular npm packages we could find. To import an npm package, simply require the desired package name, like you would in any Node.js app. Here's how to import the extremely useful underscore.js library:

                           var _ = require("underscore");

If there is an npm package that you need but is not listed on the manage code endpoint page, please email Note that certain npm packages may be rejected for security reasons.

Get Free Help Creating Your Code Endpoints

For a limited time we will help you set up your code endpoints for free! That's free engineering! Just enter your contact information and your API or data problem in the form below:

No comments :

Post a Comment