Over a million developers have joined DZone.

Playing with the Parse.com API

DZone 's Guide to

Playing with the Parse.com API

· Mobile Zone ·
Free Resource


Screen Shot 2013-10-23 at 9.29.51 PM

I’ve started using the Parse.com API not more than a week ago, and I've already fallen in love with it. The first thing that hit me was “whoa, no extra work for storing objects in my database” and that’s really it. Saving data was never easier. Parse.com’s JavaScript API, the documentation is very good and it will help you to start your project in no time. Besides diving into the details, let me highlight another cool feature of Parse.com API. It’s the user management. Sign up someone, or let someone sign in, validate their email? Everything is there!

’Nuff Said, How to Start?

Go to https://parse.com/apps, register a new application and copy the application key and JavaScript key.

Now open your HTML file, add the Parse.com’s js library

<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.12.min.js"></script>

and now initialize your Parse.com application

Parse.initialize("Application ID", "Javascript Key");

That’s it. Now you are ready to play with the rest of this article.

Sign Me Up, Pronto!

One of the most tedious, boring work while developing an application is to deal with registration, email validation, ACL and taking care of the signing in process. With Parse.com API this is pretty straight forward. To sign up a new new user, all you gotta do is the following one!

var user = new Parse.User();
user.set("username", "ElMacho");
user.set("password", "chicken");
user.set("email", "el@macho.name");
user.signUp(null, {
  success: function(user) {
    // Everything's done!
  error: function(user, error) {
    alert("Error: " + error.code + " " + error.message);

Your user is signed up! If you need to validate user’s email then log into your Parse.com dashboard and from your application’s settings section turn on the email validation. It’s simple.

All Right, Let Me In!

Alright, your users have signed up .Now let them get in. It’s simple to write a sign in script with Parse API

Parse.User.logIn("ElMacho", "chicken", {
  success: function(user) {
  error: function(user, error) {
    console.log("you shall not pass!");

But hey, does a user have to log in every time? No, not at all. The logged in user’s state is saved in browser. So before attempting to sign in him/her again, you can check if he is already logged-in.

var loggedInUser = Parse.User.current();
    //off you go, perform a log in
} else {

easy, eh?

Interesting, How Can I Deal with Data?

Okay, enough with signing up and signing in. Lets save some data. Say, for example, we want to store user’s favorite books in Parse.com’s datastore. We will also retrieve the list later.

To save some data in Parse.com, we need to write code like this

var user = Parse.User.current();
var Book = Parse.Object.extend("favoritebooks");
var b1 = new Book({"name":"King Solomon's Mine","user":user});
var b2 = new Book({"name":"Return of She","user":user});

While saving an object you can also take care of the error or success state.

var b1 = new Book({"name":"King Solomon's Mine","user":user});
b1.save(null, {
        console.log("successfully saved")

You can save as many Books/data as you want, and link them up with the current user. Linking like this is useful for retrieving them in future. As we have a few saved records, lets pull them and see if they are really saved.

var Book = Parse.Object.extend("favoritebooks");
var q = new Parse.Query(Book);
  success: function(results){
    for(i in results){
      var book = results[i];

Once it runs, you can see that it outputs currently logged in user’s favorite books. Pretty neat, eh?

like equalTo, there are a dozen of useful constraints which you can use with your query. You can get the complete list at https://parse.com/docs/js_guide#queries-constraints . Parse.com’s documentation is pretty solid, and you will find almost everything over there.

It’s Dinner Time, Log Me Out!

Simple, just call Parse.User.logOut() and you are done!

So is it Just About User Management and Saving Data?

No, not at all!. But saving data and managing credentials + ACL are one of the coolest features that you can do with Parse.com API. Beside these object storage, you can host your application (ExpressJS based), you can run your code in the cloud (Background Job, Cloud Code), take care of the push notifications and even upload and store binary files.

Overall, Parse.com’s API is super cool. Give it a try today, you will love it for sure. And by the way, there is a super handy data browser in your application dashboard, use that!


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}