Learn IBM-CLOUD-FUNCTIONS with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Simple IBM Cloud Function (Node.js)
# ibm_cloud/demo/hello.js
function main(params) {
return { body: 'Hello, IBM Cloud!' };
}
exports.main = main;
A simple IBM Cloud Function that returns 'Hello, IBM Cloud!' when invoked.
2
IBM Cloud Function with Parameters
# ibm_cloud/demo/params.js
function main(params) {
const name = params.name || 'Guest';
return { body: `Hello, ${name}!` };
}
exports.main = main;
A Cloud Function that greets a user based on input parameters.
3
IBM Cloud Function Returning JSON
# ibm_cloud/demo/json.js
function main(params) {
return { body: { message: 'Hello JSON!' } };
}
exports.main = main;
A function returning a JSON object.
4
IBM Cloud Function Handling HTTP Method
# ibm_cloud/demo/method.js
function main(params) {
const method = params.__ow_method;
if (method === 'POST') {
return { body: 'You sent a POST request' };
}
return { body: 'You sent a GET request' };
}
exports.main = main;
A function responding differently based on HTTP method.
5
IBM Cloud Function Using Environment Variable
# ibm_cloud/demo/env.js
function main(params) {
const apiKey = process.env.MY_API_KEY || 'NotSet';
return { body: `API Key length: ${apiKey.length}` };
}
exports.main = main;
A function accessing an environment variable.
6
IBM Cloud Function Redirect
# ibm_cloud/demo/redirect.js
function main(params) {
return {
statusCode: 302,
headers: { Location: 'https://example.com' },
body: ''
};
}
exports.main = main;
A function that responds with a redirect.
7
IBM Cloud Function with Custom Headers
# ibm_cloud/demo/headers.js
function main(params) {
return {
statusCode: 200,
headers: { 'X-Custom-Header': 'IBMCloudFunction' },
body: 'Custom headers added'
};
}
exports.main = main;
A function adding custom headers to the response.
8
IBM Cloud Function Fetching External API
# ibm_cloud/demo/fetch.js
const fetch = require('node-fetch');
async function main(params) {
const response = await fetch('https://api.github.com');
const data = await response.json();
return { body: data };
}
exports.main = main;
A function fetching data from an external API and returning it.
9
IBM Cloud Function Serving HTML
# ibm_cloud/demo/html.js
function main(params) {
const html = '<!DOCTYPE html><html><body><h1>Hello HTML!</h1></body></html>';
return {
statusCode: 200,
headers: { 'Content-Type': 'text/html' },
body: html
};
}
exports.main = main;
A function responding with a simple HTML page.
10
IBM Cloud Function Conditional Response
# ibm_cloud/demo/conditional.js
function main(params) {
if (params.action === 'ping') {
return { body: 'pong' };
}
return { body: 'unknown action' };
}
exports.main = main;
A function responding differently based on query parameter.