Get started

Cloud Code functions

12min

Deploy and call your first Cloud Code functions

After using the Back4App’s data store capabilities, it’s time to learn how to use Cloud Code Functions.

Cloud code allows your application to automatically run javascript-based functions on the server-side (Back4App) in response to events triggered by Parse and HTTP requests. Your JavaScript code is stored in the Back4App servers and runs in a managed environment. As a result, there’s no need to manage and scale your own servers. Database triggers, request validators, and integrations are common use cases for Cloud Code, but you can do whatever your application needs, including using any npm module in your functions.

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. ° Install Parse SDK. ° Follow the Connect to Back4App tutorial to learn how to install Parse SDK to your application.

Goal

  • To deploy and execute a cloud function from your App

1 - Check your Cloud Code page

Go to your Dashboard > Cloud Code. As you can see in the image below:

Document image


In this section, you can see two folders: cloud and public.

2 - Edit the main.js file

This file can be found inside the cloud folder. Just click on it, and a panel will open on the right side, as shown in the image below:

Document image


Cloud Code Functions must be declared inside your main.js file. If you want to declare functions in other files, you should import them on the main.js using the require('./nameFile.js')

If you already have a main.js file created, it’s also possible to upload it, by clicking on the “Upload File” button.

Document image


3 - Create your first Cloud Code Function

Let’s now create some functions inside your main.js file. Below you can find some cloud functions examples.

main.js


Cloud functions accept a JSON parameters dictionary on the request object, so we can use that to pass up the numbers in the example above. The entire Parse JavaScript SDK is available in the cloud environment, so we can use that to query over Review objects.

main.js


4 - Deploy your function to Cloud Code

After creating your Cloud Code, you have to deploy it on Back4App servers. Just click on the button, in the image below:

Document image


After concluding the deploy process you will see a success message. Click over your main.js to visualize it on the Dashboard Cloud Code preview. Note that the main.js icon color changed from orange to blue.

Document image


5 - Run the Cloud Code Function

To run the Cloud Code Function, call the method below through your application. To do so, use the code that corresponds to the technology of your project.

The code below won’t work as expected if you haven’t installed Parse SDK correctly for the technology of your project. So, check the Connect to Back4App tutorial to certify that you have installed Parse SDK correctly in your application.

JS
Flutter
Android
iOS
.NET
Unity
PHP
REST API


What to do Next?

Now you can keep exploring Cloud Functions on your preferred technology or explore and also learn how to use cloud functions to make integrations, unit tests, and more.

Conclusion

At this point, you have learned how to use Cloud Code functions to update objects stored in the Back4App database. In case you face any trouble while deploying your code, please contact our team via chat!