OpenWhisk Events
Docs Version: 1.10.0
Edit on github

#API Gateway

Apache OpenWhisk has an API gateway included within the platform. This service allows you to define public HTTP endpoints for your serverless functions.

To create HTTP endpoints as Event sources for your Apache OpenWhisk Functions, use the Serverless Framework's easy API Gateway Events syntax.

#API Gateway Events

#Simple HTTP Endpoint

This setup specifies that the hello function should be run when someone accesses the API gateway at example/hello via a GET request.

Here's an example:

# serverless.yml

    handler: handler.hello
      - http: GET hello

URL paths for the serverless functions are prefixed with the function name, e.g. /function_name/some/path.

// handler.js

'use strict';

module.exports.hello = function(params) {
  // Your function handler
  return { payload: 'Hello world!' };

When this service is deployed, the base API Gateway url will be printed to the console. Combine this with your custom HTTP path to create the full HTTP endpoint exposing your serverless function.

$ serverless deploy
Serverless: Configured API endpoint:

$ http get
    "payload": "Hello, World!"

#HTTP Endpoint with Parameters

Here we've defined an POST endpoint for the path posts/create.

# serverless.yml

    handler: greeting.handler
      - http: POST greeting/generate
// posts.js

'use strict';

module.exports.handler = function(params) {
  const name = || 'stranger';
  // Your function handler
  return { payload: `Hello ${name}!` };

The body of the incoming request is parsed as JSON and passed as the params argument to the function handler.

The returned JavaScript object will be serialised as JSON and returned in the HTTP response body.

#HTTP Endpoint with Extended Options

Here we've defined an POST endpoint for the path posts/create.

# serverless.yml

    handler: posts.create
      - http:
          path: posts/create
          method: post

#CORS Support

Note: All HTTP endpoints defined in this manner have cross-site requests enabled for all source domains.