Rest API
Moonify is an innovative solution to help you monetize your traffic.
Introduction
The Rest API of Moonify is part of the server SDK. It helps you initialize a Moonify session between your server and ours,which gives you the ability to send critical information to us like the connected user id.
You will need:
- Your Provider account
- Your service private key (Used to authenticate)
Please refer to the Getting started section, if you don’t know what it is.
Host
The host of the api is and will always be api.moonify.io
https://api.moonify.io
API Version
Latest version : 0.2
The Moonify Rest API is realeased in versions. As a Moonify provider, you will be notified when a new version comes out, or when an older version is deprecated.
The version number is reflected in the URL like this :
https://api.moonify.io/0.2/
Authentication
Almost all requests need to be authenticated using your private serviceID. There are several methods, the most practical one is to use the Authorization header like the example below.
Request example
$ curl http://api.moonify.io/0.2/moonitize/sessions \
-X POST \
-H "Authorization: serviceID yourServiceID"
Response example
[HTTP/1.1 201 Created]
{
"success": true,
"tokenID": "7d49d33b9d616b526e960094acb716d6"
}
The “serviceID” parameter passed in POST or GET can be used to authenticate too.
HTTP Methods
Moonify API uses appropriate HTTP method for each action.
Method | Description |
---|---|
GET | Used for retrieving resources. |
POST | Used for creating resources, or performing actions. |
Response
Responses will be in the JSON format.
Node Name | Description |
---|---|
error | If this property is filled, this is due to an error |
{resource name} | Includes the invoked API’s data |
HTTP Status Code
Moonify API indicate success or failure of an API call. An exhaustive list of used HTTP status codes are below.
Method | Description |
---|---|
2xx | Success |
4xx | Bad request sent to server |
5xx | Server side error |
Details :
Method | Description | Details |
---|---|---|
200 | Success | The request was successfully completed. |
201 | Created | The request was a success and the resource has been created. |
400 | Bad request | The request cannot be performed. (malformed/missing param.) |
403 | Unauthorized | The request was rejected because of an invalid serviceID. |
404 | Not found | The resource you’ve requested doesn’t exist. |
405 | Method Not Allowed | The requested resource doesn’t support the HTTP Method used. |
500 | Server error | The server has encoutered an error. Please contact us in this case. |
Rejected request response example
[HTTP/1.1 401 Unautorized ]
{
"error": "Service ID is invalid"
}
Get started
To initialize a monetization session, you have to call the ‘sessions’ method
Request example
$ curl http://api.moonify.io/0.2/moonitize/sessions \
-X POST \
-H "Authorization: serviceID yourServiceID" \
-d '{"userID":"MyUserId"}'
Response example
[HTTP/1.1 201 Created]
{
"success": true,
"tokenID": "7d49d33b9d616b526e960094acb716d6"
}
A tokenID will be given in the response. You will now have to programmatically write some Javascript code in your page for the JS SDK to take over.
<script src="https://pkg.moonify.io/moonify.min.js"></script>
<script type="text/javascript">Moonify.set({tokenID : 'INSERT_TOKENID_HERE' });</script>
Sessions
Open a session
Initialize a Moonify session (required to start monetization). You have the option to pass parameters such as userID and multiples groups. This results in the creation of a user and/or groups.
The user is always registered in the last declared group, and the score is incremented in this last group.
API Endpoint
POST /moonitize/sessions
Parameters
Parameters | Type | Description | Default value |
---|---|---|---|
serviceID | String | Your private service ID | - |
userID (optional) | String | The id of a user from your website/service | - |
@… (optional) | String | All values preceded by ‘@’ are recognized as a group | - |
Request example
$ curl http://api.moonify.io/0.2/moonitize/sessions \
-X POST \
-H "Authorization: serviceID yourServiceID" \
-d '{"userID" : "MyUserId", "@team" : "blue", "@kind" : "paladin"}'
Response example
[HTTP/1.1 201 Created]
{
"success": true,
"tokenID": "7d49d33b9d616b526e960094acb716d6"
}
Users
List of all users
Get a list and details of all users of your service.
API Endpoint
GET /moonitize/users
Limitation : 100 users per request. You should use start cursor to get next users (see params.). Sorting possibilities are limited to a few indexes. (score/desc only at this time).
Note : All statistics returned by lists are updated each 5 minutes.
Parameters
Parameters | Type | Description | Default value |
---|---|---|---|
limit | Integer | Constrain the number of items returned | 100 |
start | Integer | Start item cursor (to paginate) | 0 |
order | String | Sort column | - |
dir | String | Sort direction (“asc” or “desc”) | desc |
Request example
$ curl http://api.moonify.devel/0.2/moonitize/users\?limit=50\&order=score\&dir=desc \
-H "Authorization: serviceID yourServiceID"
Response example
[HTTP/1.1 200 OK]
{
"users": {
"martin": {
"id": "martin",
"score": 2161.6045921436,
"scoreRewarded": 109.90210949047,
"scoreRewardedByMoonify": 101.1255322047,
"totalTime": 84160,
"updated": "2018-05-17 09:01:26"
},
"betty": {
"id": "betty",
"score": 271.4609971238,
"scoreRewarded": 0,
"scoreRewardedByMoonify": 41.1255322047,
"totalTime": 4819,
"updated": "2018-03-16 17:22:04"
},
"john": {
"id": "john",
"score": 21.832955758312,
"scoreRewarded": 0,
"scoreRewardedByMoonify": 91.1255322047,
"totalTime": 478,
"updated": "2018-03-15 15:22:02"
}
}
}
Retrieve an user
Get the details of a given user
API Endpoint
GET /moonitize/users/{userid}
Request example
$ curl http://api.moonify.devel/0.2/moonitize/users/betty \
-H "Authorization: serviceID yourServiceID"
Response example
[HTTP/1.1 200 OK]
{
"user": {
"id": "betty",
"score": 271.4609971238,
"scoreRewarded": 0,
"scoreRewardedByMoonify": 91.1255322047,
"totalTime": 4819,
"updated": "2018-03-16 17:22:04"
}
}
Groups
Retrieve a group
Get the details of a given group
The score of a group represents the total score that each user has achieved in that group.
API Endpoint
GET /moonitize/groups/{groupname}/{groupvalue}/
Request example
$ curl http://api.moonify.devel/0.2/moonitize/groups/team/blue/ \
-H "Authorization: serviceID yourServiceID"
Response example
[HTTP/1.1 200 OK]
{
"group": {
"name": "team",
"value": "blue",
"score": 19.517380442180833,
"scoreRewarded": 218.454544,
"scoreRewardedByMoonify": 91.1255322047,
"totalTime": 975,
"updated": "2018-04-26 16:31:29"
}
}
List of all users in a group
Get lists and details of all users of a given group
The score of a user in a group represents the total score that this user has made in this group.
API Endpoint
GET /moonitize/groups/{groupname}/{groupvaalue}/users/
Limitation : 100 users per request. You should use start cursor to get next users (see params.). Sorting possibilities are limited to a few indexes. (score/desc only at this time).
Note : All statistics returned by lists are updated each 5 minutes.
Parameters
Parameters | Type | Description | Default value |
---|---|---|---|
limit | Integer | Constrain the number of items returned | 100 |
start | Integer | Start item cursor (to paginate) | 0 |
order | String | Sort column | - |
dir | String | Sort direction (“asc” or “desc”) | desc |
Request example
$ curl http://api.moonify.devel/0.2/moonitize/groups/team/blue/users\?limit=50\&order=score\&dir=desc \
-H "Authorization: serviceID yourServiceID"
Response example
[HTTP/1.1 200 OK]
{
"users": {
"gerald": {
"id": "gerald",
"score": 1222.6045921436,
"scoreRewarded": 218.454544,
"scoreRewardedByMoonify": 91.1255322047,
"totalTime": 8416,
"updated": "2018-05-17 09:01:26"
},
"gregory": {
"id": "gregory",
"score": 5346.4609971238,
"scoreRewarded": 218.454544,
"scoreRewardedByMoonify": 91.1255322047,
"totalTime": 98827,
"updated": "2018-03-16 17:22:04"
}
}
}
Retrieve a user in a group
Get the details of a given user in a given group
The score of a user in a group represents the total score that this user has made in this group.
API Endpoint
GET /moonitize/groups/{groupname}/{groupvalue}/users/{userid}
Request example
$ curl http://api.moonify.devel/0.2/moonitize/groups/team/blue/users/gerald \
-H "Authorization: serviceID yourServiceID"
Response example
[HTTP/1.1 200 OK]
{
"user": {
"id": "gerald",
"score": 271.4609971238,
"scoreRewarded": 218.454544,
"scoreRewardedByMoonify": 91.1255322047,
"totalTime": 4819,
"updated": "2018-03-16 17:22:04"
}
}