Developers need APIs. Every tech company that provides Internet-based services exposes an application programming interface to enable developers to do their jobs faster and smarter.
Building an API requires a team of experienced developers to consider all of the business logic, security, network load, and system costs in order to make the company successful in serving its customers.
Sign up here to get your own API keys and access to PubNub Functions so you can build your own API now.
The time and energy required to make an API can be expensive. PubNub’s product and DevRel teams are well-versed in the obstacles that an engineering team must conquer to produce an awesome API. With these challenges in mind, the PubNub Functions engineering team has built the On Request event handler feature, to enable developers to globally deploy a serverless REST API in minutes, not months.
Instead of taking the time to
…you can deploy your code to a PubNub Function event handler with the click of a button, or 1 line with the PubNub command line interface.
PubNub Functions are JavaScript event handlers that can be executed on in-transit PubNub messages or in the request/response style of a RESTful API over HTTPS. If you are familiar with Node.js and Express, the On Request event handler development will be second nature.
Deploying code can be done on the command line using the pubnub-cli
on npm and also via CI/CD like in this tutorial.
For deploying using your command line, see this tool.
PubNub Functions are serverless. There is no need to worry about deploy process, maintaining infrastructure, or scaling. We have several points of presence around the world in which your code is deployed simultaneously. This ensures that your users have an extremely low latency experience, regardless of their location.
To get started, create a forever free account at PubNub and click the Functions tab in the dashboard. Create a module and an event handler with the type on request.
Press the play button on the right and use the UI on the left for making test GET, POST, PUT, and DELETE requests.
The COPY URL button on the left gives you the public URL that this API can be accessed with. The ending path can be adjusted, but the URL itself is immutable.
The REST API examples are demonstrated in an event handler in my GitHub Repository.
Deploy the function code using the Functions CLI, or the editor in the dashboard.
Next, run the index.html file in your favorite web browser to interact directly with your REST API.
The event handler in this repo shows how to Functions into an API. There is a controller object that points to JavaScript functions for each route URL parameter.
You, the developer, assign route names to each controller like in the code snippet. The function definition should be defined for each HTTP request method used (like GET, POST, PUT, PATCH, and DELETE). The methods are keys in these objects, and the JavaScript functions are the values. The method string is converted to lowercase, so define your functions like get, post, put, patch, or delete.
The code at the very bottom selects the function to execute based on the request method and URL parameters like route. If there is not a route defined on the back end, a 404 is returned.
The following functions are controllers executed for API routes for an account. The account is a JSON object with various customer data. The Functions KV Store is used like a database, to retrieve/store the object by the specified ID. These functions show how to implement basic Create, Read, Update, or Destroy functionality in your API.
Up to 3 xhr
requests can be made in 1 event handler execution. They can be chained using JavaScript promises.
Also, an API request that needs secret API keys can be properly implemented in Functions using vault. Add secret keys to the module by clicking MY SECRETS in the event handler editor page.
I have created an HTML file that can be opened in a web browser to test out the API functionality. If you have not already, deploy your code to PubNub Functions as an on request event handler (signup for a forever free account here).
Next, copy the URL for the live REST API and paste it into the HTML file on line 50. You can find the URL in your PubNub Function event handler editor page; click the COPY URL button on the left side.
Save the file and open it with a modern browser like Chrome or FireFox.
Next, use the UI to select a radio button. Each of them corresponds to an endpoint in the PubNub Function. Selecting one prepares the proper request using vanilla JavaScript’s XMLHttpRequest. To execute, press the Send Request button. The JavaScript code writes the request and response details to the screen after you press the button.
Want to know if your API is experiencing downtime? Include the pubnub
module in your event handler, and publish to a channel whenever a .catch block executes. This can be configured with elastic search and pager duty. You can also utilize this repo for a constant monitoring application.
You can now see that PubNub Functions is very powerful. An event handler deployed with this system can handle an extremely high volume of requests at any time. Make your own REST API with this GitHub repo. Reach out to [email protected] if you have any questions or need some advice.
Originally published at www.pubnub.com.