Why Do Childhood Memories Vanish?

Think back. Way back to your earliest memories. To the ones that you know are purely yours: uncontaminated by photo evidence or retelling by friends or family. If you can’t remember back to being an…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Create Serverless Cloud Functions with Managed REST APIs and API Keys

Cloud Functions are maturing very very fast! It takes no more than 15 minutes to create a fully auto-scalable and securely managed API endpoint to access a single or a sequence of Cloud Functions that are access restricted by a professional developer portal with API key management. That’s a pretty powerful feat for any developer.

The goal here is to create a single REST API endpoint ‘POST /chat’ and a sequence REST API endpoint that triggers Push Notifications of received requests to a WebSocket connection at ‘ws://hostname/ws’. The WebSocket endpoint logs received notifications into a MongoDB database. Finally, I want to secure access to my APIs by API keys with an API manager, in this case API Connect.

To set this up I execute the following steps in Cloud Functions:

But first, I created a Node.js server using Node-RED without Cloud Functions that implements the following flows:

To install and setup the above flow in Node-RED follow these instructions:

In this article however, I am recreating the part that executes the ‘Push Notification Server’ workflow, but this time as a Cloud Function with an API Manager to control access to the endpoints using API keys.

In the ‘Bluemix Dashboard’, from the top-left menu, click the ‘Functions’ item.

In the ‘Overview’ page of the ‘Functions’ dashboard, in the left menu, click the ‘Develop’ item.

This brings you to the Cloud Functions developer IDE.

Cloud Functions — Developer IDE

Click the ‘Create an Action’ button.

For ‘Choose a name for your action’ enter ‘chat’, under ‘Choose an Execution Runtime’ select ‘Node.js’, under ‘Select a sample (or blank) to start with’ choose the ‘Hello World in JavaScript’, and click the ‘Create Action’ button.

Change ‘message’ to ‘msg’ as ‘message’ is a reserved word that will cause problems later in the setup. Click the ‘Make It Live’ button to deploy your changed Cloud Function.

Next, I create a Sequence if Functions and use the

Click the ‘Link into a Sequence’ button at the bottom of the ‘chat’ function editor. In the ‘Configure a New Action Sequence’ window, you see a list of existing packages. Scroll down and click the ‘WEBSOCKET’ package.

Click the green ‘NEW BINDING’ hexagon.

Name the new binding or package ‘ChatPushPackage’, and in the ‘uri’ input control enter the WebSocket connection string, e.g. ‘ws://remkohdev-nodered.mybluemix.net/ws’ to which the sequence should connect to send Push Notifications.

Click the ‘Save Configuration’ button.

Click the ‘Add to Sequence’ button. Review the draft of the new Sequence.

And click the ‘This Looks Good’ button. Name the new Sequence ‘ChatPushSequence’, click ‘Save Action Sequence’, and click ‘Done’.

We need to make an important change to the ‘chat’ action now! Edit the ‘chat’ action, and change the return message by replace the ‘msg’ node by a ‘payload’ node, in the response message. This is necessary, because the WebSocket package in the sequence expects a ‘payload’ node to pass the input.

Click the ‘Make It Live’ button to deploy the changes in the ‘chat’ action.

At the bottom of your ‘chat’ action, click the ‘View REST Endpoint’.

Click the ‘Show Full Command’ link. Copy the expanded full cUrl command, including the Authorization string, to test your API.

Close the ‘View REST Endpoint’ window. Click the ‘Monitor’ item in the left menu, and review the ‘Activity Log’ to check your Function is running.

Under ‘MY SEQUENCES’ click the ‘ChatPushSequence’ sequence, and click the ‘Run this Sequence’ to test the sequence set up. Enter a JSON string under the ‘JSON Input’.

Click the ‘Run with this Value’ button. You should see your test run finish with a green bar to indicate it ran successfully.

Click the ‘Close’ button in the bottom right, and in the top right click the ‘View Sequence Details’.

Under ‘Web Action’ check the ‘Enable as Web Action’ checkbox. Copy the ‘Web Action URL’ and append the URL parameter ‘msg’.

Run the full Web Action URL in your web browser.

Back in the Cloud Functions manager, in the left menu, under the ‘Triggers’ item you can connect this action sequence with Triggers in Cloud Functions.

Click ‘Manage’ in the breadcrumb, click the ‘APIs’ item in the left menu, and click the ‘Create an OpenWhisk API’ button.

In the ‘Create API for Cloud Functions’ window, in the ‘API Info’ window, under ‘API name’ enter ‘ChatPushAPI’, and under ‘Base path for API’ enter ‘/api’.

Click the ‘Create operation’ button.

Under ‘Path’ enter ‘/chat’, select ‘POST’ under ‘verb’, and under ‘Action’ make sure the ‘chat’ action is selected. Click the ‘Save’ button.

You created a REST API endpoint for JUST the ‘chat’ action. Create a second operation that calls the sequence now!

Click the ‘Create operation’ button.

Under ‘Path’ enter ‘/chatpush’, select ‘POST’ under ‘verb’, and under ‘Action’ make sure the ‘ChatPushSequence’ action is selected. Click the ‘Save’ button.

In the ‘Security and Rate Limiting’ window, switch on the toggle for ‘Require applications to authenticate via API key’.

Optionally, you can decide the limit the rate by switching on the toggle for ‘Limit API call rate on a per-key basis’. For the purpose of this article I am not switching it on, but you could.

Scroll down and click the ‘Save’ button.

In the left menu, click the ‘Sharing’ item.

Make sure that the ‘Expose Managed API’ toggle is active. In the ‘Sharing Outside of Bluemix Organization’ window, click the ‘Create API key’ button. Enter a ‘Descriptive name’ for the API key, e.g. ‘ChatPushPublicAPIKey’ and click the ‘Create’ button. Refresh or reload the page if you don’t see the API key immediately.

Creating the API key, also created an ‘API Portal Link’. Click the ‘API PORTAL LINK’ to go to the Developer Portal.

Copy-paste the API key into the ‘X-IBM-Client-ID’ HTTP header under Identification,under ‘Parameters’ in the ‘body’ window enter a JSON message.

and click the ‘Call operation’ button. You should get a Response code: 200 with a Response body.

Now, test the ‘chat’ operation, and note that the sequence is not triggered this time.

Congratulations, you released an API key managed API into production with a fully scalable Cloud Function ready for the year 2018.

Add a comment

Related posts:

Working with Principals

I start learn to know about computer since elementary school. During that time I always amaze how big principals is doing the research and development and also how they manage to deliver their…

Buy Google Voice Accounts

Google has been doing a commendable job in terms of its services. It has provided us with the things necessary for marketing in business. When we have engagements to do regarding our business deals…

Improving the Virtual Learning Experience for Elementary Students

Following more than two decades as a teacher and school leader, Louise Losos started her role with Confluence Academies as the Executive Director of Curriculum and Assessment. In this role, Louise…