Creating your first REST API service using Parse

· Tech · , , , ,

Parse is a great cloud service with lots of features, one them being Parse Cloud Code. Let’s say you have some business logic that you don’t want expose to the public – that’s where Parse Cloud Code comes in. It is basically a dead simple way to create your own API back-end service using nothing but JavaScript. In this example I’ll be using AngularJS to test the API service. Before we start make sure to create a Parse account, at least created one app and to have the latest Parse Console app.


1. Open ParseConsole.cmd and enter following command. This command line creates a folder with three subfolders (cloud, config, public). During the creation, you’ll be prompted to enter email, password and to choose app :

parse new FirstCloudCode

2. Go to the cloud folder and open main.js. Here you’ll find an already defined API service called hello. Erease all of the text and type the following code:

Parse.Cloud.define("randomNum", function(request, response) {
var min = parseInt(request.params.min);
var max = parseInt(request.params.max);

var number = Math.floor(Math.random() * (max - min + 1) + min);


This API service receives two parameters (min and max) – generates a random number between min and max number.

3. Once you’ve defined your first Parse Cloude Code, you’ll need to upload/deploy it to your Parse app using the ParseConsole.cmd. Make sure that you’re inside the root folder (see step 1). Enter the following code:

parse deploy

4. Your first REST API service is now ready. Let’s try it out using AngularJS. Before we start with the code, make sure to collect your Parse Application ID and Parse REST ID from your Parse App.

We have created following files with following code:


<pre>parseApp.service("ParseService", function ($http, $q) {
var ParseURL = "";
var appID = "<INSERT APP ID>";
var restID = "<INSERT REST ID>";
var defer = $q.defer();

//Set up header values
$http.defaults.headers.common['X-Parse-Application-Id'] = appID;
$http.defaults.headers.common['X-Parse-REST-API-Key'] = restID;
$http.defaults.headers.common['Content-Type'] = "application/json";

//Call Parse Cloud Code
this.genRandomNum = function () {
"min": "1",
"max": "150"
).success(function (result) {

}).error(function (error) {
return defer.promise;


<pre>parseApp.controller("ParseCtrl", function ($scope, ParseService) {
$scope.number = results.result;


<pre><!doctype html>
<html lang="en" ng-app="parseApp">
<meta charset="utf-8">

<body ng-controller="ParseCtrl">
<h1>RANDOM NUMBER = {{number}}</h1>

<script src="lib/angular/angular.min.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers/ParseCtrl.js"></script>
<script src="js/services/ParseService.js"></script>
Written by almirmesic · · Tech · , , , ,
NextHost your AngularJS app using Parse in 4 simple steps