BaaS REST API 

This document describes how to interact with the MakeItApp BaaS REST API for CRUD operations with custom collections configured via Dashboard (see the BaaS introduction for details).


Request the BaaS for your App 

Ask your App Angel to create a new BaaS for you.
You will then be able to see the BaaS Url and the Private Key in the Tools Tab of your MakeItApp Project .

Schermata 2014-05-06 alle 17.11.40

Now you can access to your BaaS using the Dashboard and  create a collection.


APIs 

Collections can be managed using REST calls. All requests to the BaaS are under https and requires a key on the header. You will receive the key when your App reaches the Development Status.

Please note: all names are case sensitive!

secret:YOUR_KEY

Data Filtering 

You can use all MongoDB query syntax to filter data on our BaaS. Here details about filters on MongoDB.

GET /:collection?{MONGO_FILTER}

Examples:

Filter by field (get cards with color red)

GET /:card?{"color":"red"}

Filter by regex (get cards with colors that start with r)

GET /:card?{"color":{"$regex":"r"}}

Getting data 

To retrieve a single element of the collection:

GET /:collection/:ID

To retrieve all elements of the collection:

GET /:collection

Example with CURL
Using curl – http://curl.haxx.se/download.html – you can manage your collection:

URL='YOUR_BAAS_URL'
FILTER=$URL'/YOUR_COLLECTION_NAME'
SECRET='secret: YOUR_KEY'
curl --header $SECRET -H Accept: application/json -H Content-Type: application/json -X GET $FILTER

Paginating Data 

Pagination uses “skip” and “limit” to identify pages. “Skip” identifies the number of records to skip from the start, “Limit” the max result. If you have 300 results and you want to obtain the third page with a dimension of 50 records set skip to 100 and limit to 50.

GET /:card?{"$skip":100,"$limit":50}

Sorting Data 

Sort by card name:

1: ascending.
-1: descending.

GET /:card?{$sort":{"card":1}}

Comparison Operators 

From Mongo website
$gt Matches values that are greater than the value specified in the query.
$gte Matches values that are equal to or greater than the value specified in the query.
$in Matches any of the values that exist in an array specified in the query.
$lt Matches values that are less than the value specified in the query.
$lte Matches values that are less than or equal to the value specified in the query.
$ne Matches all values that are not equal to the value specified in the query.
$nin Matches values that do not exist in an array specified to the query.

Get players with revenues greater that $100:

GET /:player?{"revenue":{"$gt":100}}

Registration 

You can register new users by using the “SECRET” code as in the example below:

SECRET='secret:d37fbfda907b928c2ewwe6d55bc09d73fb9675285bd5bb5fff5403bd35e9b400afb59c8891a418b1398b89c96169c45a2755787d56e8b9df814aee1172448158291a039c65b324db753af237ae3186a4d18bc6830a876df69360f7aadf7c5ea310e491c43952621b8f5cdf44cd666d4ee5d57ebf35bee780a9823696cffe045848a161426a2a935af13cecb2eb67d9cf45b3b95dfc4d454d4a3bfcbd937b70751f02fd7cead09de1f0282639191e2ef75539ca99a05ffcb8fe527a9e2cfd9f40b3f4ab3ce93580806c23290ae7cb68895578eafa64eb16ebd64b3a323d11f40af5b48c4da10df1e4e2cc334a09ff91cac9cc9fbc73bca97bcbd490c63a457385'
#register
REGISTER=$URL'/users/'
#curl -H "Content-Type: application/json" -X POST -d '{"username":"giulio3","password":"pippo","email":"giulio.roggero@fastwebnet.it"}' $REGISTER
curl --header $SECRET -H "Content-Type: application/json" -X POST -d '{"username":"traffico","password":"pippo!","email":"pippo@makeitapp.eu"}' $REGISTER

User Login 

Set up the User in the BaaS Table. (You can create the User from the dashboard or using REST Post.)

Screen Shot 2014-04-01 at 21.23.58
In the example. User: THE_USER. Password: THE_PWD.

User login:

URL='http://localhost:3000'
SECRET='secret:secret'

login:

LOGIN=$URL'/users/login'
ME=$URL'/users/me'
USR='giulio.roggero@gmail.com'
PWD='reset'
curl --header $SECRET -H "Accept: application/json" -X POST -d 'email='$USR -d 'password='$PWD $LOGIN

The result will be:

{"path":"/users","uid":"e7233e27508feb0f","id":"403a2d32c06cffcc631b769f809bcd02833132cd06e08232daebf8f6cb85ad7c62f548f60b228494cc5595b35692a36758bd4ee305b2a08e997f2892d5b56832"}

You can use the id in the cookie:

URL='http://localhost:3000'
SECRET='secret:f0167add54502dfd0456a30'
LOGIN=$URL'/users/login'
ME=$URL'/users/me'

USR='test'
PWD='test'
 
COOKIE='403a2d32c06cffcc631b769f809bcd02833132cd06e08232daebf8f6cb85ad7c62f548f60b228494cc5595b35692a36758bd4ee305b2a08e997f2892d5b56832'
 
curl --header $SECRET -H "Accept: application/json" --cookie "sid=$COOKIE" -X GET $ME

Obtaining the description of the user or the access to collections protected by a login (you can protect a collection):

{"username":"giulio","email":"giulio@giulio.it","validatedEmail":true,"id":"e7233e27508feb0f"}

Password retrive 

The BaaS supplies a retrive password API:

#user query
URL='http://127.0.0.1:3000'
SECRET='secret:secret'

#query
RES=$URL'/users/forgot'
INFO='{"email":"giulio.roggero@gmail.com"}'
curl --header $SECRET -H "Content-Type: application/json" -H "Accept: application/json" -d $INFO -X POST $RES

You can also re-send the confirmation email by using this code:

#user query
URL='http://127.0.0.1:3000'
SECRET='secret:secret'

#query
RES=$URL'/users/resend'
INFO='{"email":"giulio.roggero@gmail.com"}'
curl --header $SECRET -H "Content-Type: application/json" -H "Accept: application/json" -d $INFO -X POST $RES

Uploading Files 

The BaaS also supports the upload/download and deletion of files using the REST api :files.
To upload a file it’s necessary to send a multi-part request to the API. Here is the example with CURL…

curl --header $SECRET -F file=@$myFile $URL/files/$(basename "$myFile")

…where $URL is the baseurl of your BaaS and $SECRET the secret key.

of you can also use the COOKIE this way:

curl --header $SECRET --cookie "sid=$COOKIE" -F file=@$myFile $URL/files/$(basename "$myFile")

The API replies with a JSON that return the result and all information about the file. Locations contains the url that you can use to download the file.

{"message":"inserted","id":"86e56055-425a-46eb-a4a4-206bbbb56351.jpg","fileName":"67d892e2-7737-4daf-9723-aacaed17f70a.jpg","size":841416,"location":"https://THE_URL"}

You can get the complete list of files with a GET to files collection:

curl --header $SECRET $URL/files

A Complete Example 

Put all together:

GET /:card?{"$skip":0,"$limit":50,"$sort":{"card":1},"card":{"$regex":"c"}}
Post data
FILTER_DATA=
curl --header $SECRET -H "Accept: application/json" -H "Content-Type: application/json" -d $FILTER_DATA -X POST $FILTER
Delete data
curl --header $SECRET -H "Accept: application/json" -H "Content-Type: application/json" -X DELETE $FILTER