Compute Services

Exposing OpenWhisk RESTful APIs with API Gateway

Share this post:

openwhisk-hero-image API GatewayWith the recent promotion of OpenWhisk to general availability, you not only get the incredibly powerful, scalable, event-driven programming model for building your actions (functions) using serverless computing, but something else slipped into the release that you may not have noticed:

The OpenWhisk API Gateway

The API gateway is an experimental new feature that enables you to easily expose your OpenWhisk actions as RESTful endpoints. You can assign actions to specific endpoints, and even have verbs (get, put, post, delete) from the same endpoint assigned to different actions. You can already consume OpenWhisk actions from other applications using the JavaScript or iOS/Swift client SDKs, and now the API gateway gives you an easy path to expose your OpenWhisk actions to other non-credentialed applications.

There will be two different approaches you can to expose your actions with the API gateway:

  • Assigning API endpoint/verb combinations to specific actions individually
  • Using a Swagger config file to map API endpoints to actions (currently still in development)

Note: This is different from the ability to expose your OpenWhisk actions as managed APIs using API Connect.

A simple action: Fibonacci generator

Next we will examine how to expose an action as a REST API, but first let’s take a look at the action itself. I’m starting with a fibonacci generator. It’s a relatively simple action that generates numbers in a fibonacci sequence, where every number after the first two is the sum of the two preceding values. When invoking this action from the command line, you specify a num parameter (for the Nth place in the sequence), and it will return the value, the complete sequence, and the number of recursive invocations of the fibonacci method:

$ wsk action invoke fibonacci -p num 5 -b -r
   "n": 5,
   "value": 8,
   "sequence": [1, 1, 2, 3, 5, 8],
   "invocations": 9

You can download it and check it out for yourself: fibonacci.js

Mapping actions to endpoints

Now, let’s examine how a specific action can be associated with an API endpoint/verb. From the OpenWhisk CLI, you must specify a base path, the api path (path component for the action), the verb (get, post, put, delete), and then the action.  See more variations in the docs.

In this case, the base path is is /api-demo/v1, the api path is /fibonacci, the verb is get, and the action is fibonacci:

$ wsk api-experimental create /api-demo/v1 /fibonacci get fibonacci
ok: created API /api-demo/v1/fibonacci GET for action /_/fibonacci

This exposed my fibonacci action as a get request for the URL shown directly above. You can check it out with different fibonacci values here:

Parameters that are passed via the query string will be available in the args object passed into the action’s main function.  For example, a query string parameter ?num=10 will be passed into your OpenWhisk args as:


View the fibonacci links above to see how I am passing data into this API endpoint.

Next Steps

If you haven’t already, be sure to download the latest command line interface. For additional information, be sure to check out the docs within the OpenWhisk project’s repo, and you can always run wsk api-experimental —help for ore detail in the CLI. Keep in mind, this is an early, experimental feature that provides users an early opportunity to try it out and provide feedback. We’ve already received several enhancement requests, but definitely let the team know if there are other things you’d like to see added.


How does the OpenWhisk API gateway compare with the gateways, DataPower and Microgateway, that are offered in API Connect?


    API Connect’s gateways has throttling and analytics features as part of an API Management solution.

    OpenWhisk API gateway follows a simple api-gateway pattern.


Is this available on Ubuntu?

I could successfully create and use the api-experiment using vagrant but on ubuntu I get a 502 when trying to access it, even the creation of the api seems to be working.

Also, I noticed that the api gateway installation is missing from the instructions available for ubuntu. I suppose this is on purpose.

Can you please clarify?

Comments are closed.

More Trending Stories

WebSphere on the Cloud: Application Mitigation

Do you have a modernized WebSphere application in your business running on your on-premise infrastructure? If you’ve migrated your old application code to the present-day WebSphere Application Server (WAS) version and updated your development environments / delivery pipelines to modern practices, it’s time to move your application to the WebSphere Application Server as a Service. In the last blog entry, we explained how you can modernize your existing WebSphere applications with minimal changes and migrate them to the cloud, lowering your costs and simplifying maintenance. This blog entry, we'll cover how to iteratively moving core pieces of compute-based business logic to cloud-based services.

Continue reading

WebSphere on the Cloud: Evolving to Microservices

Do you have existing monolithic Java/JEE applications running on WebSphere Application Server? Is your application comprised of multiple business functionalities, but it’s packaged as a single application? Are there performance bottlenecks that you are not able to resolve because the application does not scale well? If you answered yes to any of these questions, read this post to learn how you can refactor your existing monolithic WebSphere application into a microservices-based application.

Continue reading

Rapidly developing applications (part 6): designing and versioning APIs

In part 6, we review best practices for creating and maintaining APIs within an application and between a cloud-deployed application and components that live on premises. This is a guide to the overall series.

Continue reading