Get started

Cloud Code functions

12min

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.

Document image




2 - Edit the main.js file

The main.js file is where your Cloud Code functions are defined.

Document image


If needed, you can import functions from other files using:

main.js


3 - Create your first Cloud Code Function

Some basic function examples include:

  • A simple greeting function:
main.js

  • A function to sum two numbers:
JS


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:

JS
Flutter
Android
iOS
.NET
PHP
REST API


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

  • Advanced Queries: Retrieve information directly from the database:
main.js


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.