Get started
Cloud Code functions
12 min
cloud code is a powerful tool that enables you to execute javascript functions directly on the server, adding advanced features to your application without the need to manage your own servers running in back4app’s environment ensures scalability and simplicity with cloud code, you can automate database actions in response to events create custom validations for requests integrate your application with external services using npm libraries prerequisites to complete this tutorial, you will need an app created at back4app follow the create a new app tutorial to learn how to create an app at back4app goal to deploy and execute a cloud function from your app 1 access your cloud code go to the cloud code section in your back4app dashboard you’ll find two main folders cloud and public 2 edit the main js file the main js file is where your cloud code functions are defined if needed, you can import functions from other files using main js require(' /filename js'); 3 create your first cloud code function some basic function examples include a simple greeting function main js parse cloud define("hello", async (request) => { console log("hello from cloud code!"); return "hello from cloud code!"; }); a function to sum two numbers parse cloud define("sumnumbers", async (request) => { return request params number1 + request params number2; }); 4 deploy your code to the server once your functions are ready, click the deploy button to publish them to the back4app environment 5 test your cloud code function you can test your functions directly via the api using tools like curl or any preferred sdk below is an example for calling the hello function parse cloud run('hello') then((result) => { console log(result); // output "hello from cloud code!" }) catch((error) => { console error('error ', error); }); flutter parsecloudfunction function = parsecloudfunction('hello'); parseresponse response = await function execute(); if (response success) { print(response result); // output "hello from cloud code!" } else { print('error ${response error message}'); } android parsecloud callfunctioninbackground("hello", new hashmap<>(), new functioncallback\<object>() { @override public void done(object result, parseexception e) { if (e == null) { log d("cloud code", result tostring()); // output "hello from cloud code!" } else { log e("cloud code error", e getmessage()); } } }); ios parsecloud callfunction("hello", parameters nil) { result in switch result { case success(let response) print("response \\(response)") // output "hello from cloud code!" case failure(let error) print("error \\(error localizeddescription)") } } net var result = await parsecloud callfunctionasync\<string>("hello", null); console writeline(result); // output "hello from cloud code!"use parse\parsecloud; try { $result = parsecloud run("hello"); echo $result; // output "hello from cloud code!" } catch (exception $ex) { echo "error " $ex >getmessage(); } rest api curl x post \\ h "x parse application id application id" \\ h "x parse rest api key rest api key" \\ \ data urlencode "" \\ https //parseapi back4app com/functions/hello 6 additional features data manipulation create, edit, or retrieve objects in your database with specific functions, such as this example for creating a todo item main js parse cloud define("createtodo", async (request) => { const todo = new parse object('todo'); todo set('title', request params title); todo set('done', request params done); return await todo save(); }); advanced queries retrieve information directly from the database main js parse cloud define("getlisttodo", async (request) => { const query = new parse query("todo"); query equalto("done", true); query descending("title"); return await query find(); }); conclusion with cloud code, you can effortlessly build robust and customized solutions it’s ideal for automation, integrations, and validations and works seamlessly with any technology, such as flutter, react native, or rest api if you encounter any issues, the back4app support team is available to assist you