Sometimes you may need to call a Cloud Code function via a URL, passing parameters and handling responses. Below is a simple way to achieve this.
1. Enable Web Hosting
First, make sure your Web Hosting is enabled. You can follow the instructions here: https://help.back4app.com/hc/en-us/articles/360002120991-How-can-I-activate-a-webhosting-at-Back4App-
Once enabled, your app will have a subdomain like: <app_name>.b4a.app
2. Create a Custom Route
Add a custom route in your app.js file.
GET Example
app.get('/my-endpoint', async (req, res) => {
// Add your logic here
res.status(200).json({ message: "hello world!" });
});POST Example (Calling a Cloud Code Function)
app.post('/my-endpoint', async (req, res) => {
try {
const data = req.body;
const response = await Parse.Cloud.run("cloud-code-function-name-here", data);
res.status(200).json({ success: true, data: response });
} catch (error) {
console.error("Error:", error.message);
res.status(500).json({ success: false, error: error.message });
}
});3. Call the Endpoint
You can access your endpoint using:
https://<your-subdomain>.b4a.app/my-endpoint
4. Alternative Method (Not Recommended)
You can also call a Cloud Function directly via URL by passing your keys:
https://<appID>:javascript-key=<JSKey>@<app_name>.b4a.app/functions/myTestFunction
⚠️ This approach is not recommended, as it exposes your keys.
5. Example Using cURL
curl -X POST https://<appID>:javascript-key=<JSKey>@<app_name>.b4a.app/functions/myTestFunction
Make sure to replace all values inside < > with your actual app credentials.
Feel free to reach out if you need any further assistance.
Comments
0 comments
Please sign in to leave a comment.