Let's say you want a web API in the form of https://hostname/api/v1/constacts/:id
The OpenWhisk Action shows an example on how to handle a http request with GET
and path /constacts/:id
const UrlPattern = require('url-pattern');
const pattern = new UrlPattern('/api/v1/contacts/:id');
function main({__ow_path:path}) {
return { body: findContactById(pattern.match(path)) }
}
- Install and Configure CLI
bx wsk
using IBM Cloud CLI Functions pluginbx wsk list
- Install nodejs deps:
npm install
npm start
npm run build
npm run deploy
npm run all
npm run api
API info display including URL
Action: /[email protected]_demo/getContact
API Name: contacts
Base path: /api/v1
Path: /contacts/{id}
Verb: get
URL: https://<apigatewayhost>/<tenantid>/api/v1/contacts/{id}
Open a browser, or use tool of choice using the URL
from previous step
curl https://<apigatewayhost>/<tenantid>/api/v1/contacts/1234
Output: Found contact from data store with id 1234
return resource with content-type application/json
and statusCode 200
{
"firstName": "Carlos",
"lastName": "Santana",
"twitter": "csantanapr",
"id": "1234"
}
Using IBM Cloud API Management you can map the URL to your cutom domain. For example uploading a SSL certificate generated using something like Let's Encrypt and mapping the API to your custom domain name. Then you can have the API endpoint on your domain (i.e. https://example.com//api/v1/contacts/{id})