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.
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
- choose a cloud hosting provider
- purchase a domain and set up DNS
- configure a Kubernetes cluster
- Dockerize application code and microservices
- deploy code globally to several points of presence
…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.
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 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.
CRUD methods for a REST API
Return an HTML response
Proxy one or many external API requests
Up to 3
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.
Test out the REST API
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.
Alerting and Monitoring
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@example.com if you have any questions or need some advice.
Originally published at www.pubnub.com.