Build an API for microservices in 5 minutes

With tools such as AnyPresence’s JustAPIs, you can define and deploy modern, RESTful APIs in no time; here’s how

In 2002 (yes, more than a decade ago), Jeff Bezos, CEO of Amazon, famously mandated that all internal development teams at the company must expose their data and functionality through Web services interfaces that can easily be consumed. He further specified that there would be no other form of communication other than via these APIs, that they must be “externalizable,” and that anyone who does not follow this edict will be fired.

One might question his management style or whether this makes sense for all internal systems, but you can’t deny the massive growth and success of AWS that sprung out of this architecture. Today, Amazon’s market share of the growing $16.5 billion cloud infrastructure services space is greater than all of its competitors combined.

Earlier, in the late '90s, enterprise software CEOs including Oracle’s Larry Ellison touted the rise of microservices using an older term, “service-oriented architecture,” or SOA. At the time, SOA patterns made sense conceptually, but enterprises had a difficult enough time transitioning from client-server to Web-based applications, and SOA was largely considered “nice to have.” In 2007, Ellison noted that uptake of SOA was slow:

People have to understand when you have a fundamentally new computer software architecture, SOA, it takes a long time for adoption… Moving to SOA is not as easy as flipping a switch.

How times have changed. While moving entire legacy IT systems to a microservices-based architecture might not be as easy as flipping a switch, creating new APIs should be that easy, and it behooves companies of all sizes to make this a goal and core competency to remain agile and competitive.

APIs and microservices as a core competency

APIs are especially important in the world of modern enterprise and consumer software where ecosystem adoption drives success of the solution. APIs dictate how developers can create new apps that tap into other services, be they social networks like Facebook and Twitter or enterprise utilities like Dropbox and Slack.

Currently, there are three primary options for developers looking to build APIs, each with their corresponding pros and cons:

  1. Do it yourself. Developers can build APIs from scratch on their own. This allows the most flexibility and control, but it takes time to build all of the foundational components and it requires you to think about how to scale and manage the APIs.
  2. Use an MBaaS solution. This is a viable option that can help you save a significant amount of time and provides the infrastructure to scale your APIs. However, you have no control over how the signatures (headers, query parameters, and so on) of the APIs are defined, and cloud-based MBaaS design tools cannot easily connect to systems behind firewalls or run locally on a laptop for rapid development.
  3. Use an API management platform. Some API management solutions offer tools and frameworks to build new APIs. These are powerful tools that also provide capabilities for governing, scaling, analyzing, and even monetizing your APIs. However, these solutions are often quite complex and expensive to deploy and use -- not quite the “flip a switch” easy option we are looking for.

A new option for easy API building

There are newer solutions on the market, such as AnyPresence JustAPIs, that enable developers to quickly define and deploy APIs within minutes. You don’t have to be a back-end architecture expert to use this type of tool, which includes a Web-based administrative console of quick declarative API definitions, familiar JavaScript syntax for business logic, and a high-performance server binary for Windows, Linux, or Mac OS X that can easily run on a laptop or be deployed in production to any cloud or on-premise infrastructure. If you have an API management solution, you can deploy JustAPIs behind your API gateway to govern and monetize the APIs created.

The benefit to solutions like JustAPIs is the broad variety of use cases they enable. For example, you can use JustAPIs for the following:

  • Build or prototype new APIs from scratch, try different designs for A-B testing, and see which ones developers like the most
  • Build modern, RESTful APIs that make multiple calls to legacy Web services and combine the data into a single JSON (or any other format) response
  • Build APIs that mimic the signature of your production APIs, add sample response data, and use them for innovation days or hackathons
  • Build a proxy API layer on top of old back-end servers while you migrate to a new back end, minimizing impact to client apps code

Once developers have an efficient, scalable platform and process to build and deploy APIs, it opens up many possibilities for innovation within an organization. Whether you are building APIs for external consumption, to enable internal cross-platform apps, to expose IoT device controls, or to implement a microservices architecture for a pet project, rapid API building and deployment is a core competency that all modern developers can benefit from.

Build an API in five minutes

Enough talk, let’s roll up our sleeves and start building our microservices core competency. This brief, hands-on tutorial shows you how to create a new API with AnyPresence JustAPIs. Before you start, download the free trial version of JustAPIs and follow the Quick Start Guide to set it up.

We will be creating a Proxy Endpoint that uses JavaScript Logic Component to return a simple “hello world” message with JSON format.

(Important note: The instructions in this tutorial refer to the sample Proxy Endpoint called “A JSON Hello World proxy” in the sample API that you imported when following the Quick Start Guide steps, and they assume you have the JustAPIs server running. If you have not imported the sample API or started JustAPIs, please do so now.)

Step 1. Find A JSON Hello World Proxy, which should appear in the list of Proxy Endpoints, and click the edit icon that appears when you hover over the row:

JustAPIs step1

Step 2. When you are in edit mode, you will see that this Proxy Endpoint has several properties that were specified when it was created:

JustAPIs step2

There are a number of points to note here: 

  1. The Proxy Endpoint’s name is A JSON Hello World Proxy, which is used for display purposes in the Admin Web App.
  2. The Active status is set to on, which means this Proxy Endpoint will run if the server receives a request for it.
  3. This Proxy Endpoint is assigned to the Dev environment, and any environment variables used in will be populated from the Dev environment.
  4. This Proxy Endpoint is assigned to the Hello World group.
  5. There is one route defined, which is a Get method at /helloworld.json. This means that if the Host is http://localhost:5000, the full URL for this Proxy Endpoint is http://localhost:5000/helloworld.json.

Step 3. Note there is one step in this Proxy Endpoint Workflow that is a Logic Component. Click on the Logic Component icon to view the details of the first step in the workflow:

JustAPIs step3

You will see that this Logic Component is composed of several substeps:

  1. Conditional is set to If, which means that any JavaScript logic specified in the Conditional Logic block must evaluate to true to continue. Otherwise, the workflow will skip to the end.
  2. There is no Conditional Logic specified, so the workflow will move to the next substep.
  3. The Logic block has two lines of JavaScript. The first line is:

response.headers["Content-Type"] = "application/json"; 

This first line specifies that the Response Object’s HTTP header “Content-Type” should be set to the value application/json. This is the standard setting when returning JSON formatted responses via HTTP, and it’s what you will typically use when creating RESTful API responses.

The second line sets the body of the Response Object:

response.body = JSON.stringify({message: "Hello World"});

This line uses a JavaScript helper method called JSON.stringify that converts a JavaScript value to a JSON string (more information here). The actual string we are returning is {message: “Hello World”}.

Step 4. Now let us test this Proxy Endpoint. Open a new browser window or tab and invoke the Proxy Endpoint using the following URL (if you have changed the default host, please use your new hostname instead):

http://localhost:5000/helloworld.json

You should see the resulting message in your browser window:

JustAPIs step4

Congratulations, you have built your first API! OK, it’s not exactly flipping a switch, but it's darn close. You can, of course, flip the Active switch on your Proxy Endpoint to turn it on or off.

Now that you know how to build and deploy APIs faster, there are no longer any barriers to becoming a microservices expert within your organization, whether you work for a one-person startup or run a 100-person IT group at a large corporation. You can try out the other JustAPIs tutorials available and let the world know about the awesome APIs you've built.

Richard Mendis is co-founder of AnyPresence.

New Tech Forum provides a venue to explore and discuss emerging enterprise technology in unprecedented depth and breadth. The selection is subjective, based on our pick of the technologies we believe to be important and of greatest interest to InfoWorld readers. InfoWorld does not accept marketing collateral for publication and reserves the right to edit all contributed content. Send all inquiries to newtechforum@infoworld.com.

From CIO: 8 Free Online Courses to Grow Your Tech Skills
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.