What’s new in Windows Azure Mobile Service : Api Script


today, when using the azure mobile service, I’ve seen a new tab called Api on the portal :


Let’s dig into this new capability.

First of all, you need to create a new api, this will create a new endpoint on your Mobile service.


Create a new Api endpoint

When you create a new api you have to fill the following parameters :

  • Name of the Api, this will create an endpoint with the url : https://{namespace}.azure-mobile.net/api/{nameOfTheApi}
  • The permission on the HTTP Method. These permissions are the same as the table permissions (Ie: Everyone, Application Key, Authenticated User, Admin & Script)


As we can see, we have the ability to use a new method: PUT.

Configure the Api

Once you’ve created the api endpoint, you’ve access to the scripts. Contrary to the Table Script (or Data Script), all your code is register in the same file. The blank script is composed with the following code :

exports.post = function(request, response) {
// Use "request.service" to access features of your mobile service, e.g.:
//   var tables = request.service.tables;
//   var push = request.service.push;
response.send(200, "Hello World");


Extend the Api

If you want to add a method, you just have to add an exportable function. For example, if you want to add the Get Method, you add the following code :

exports.get = function(request, response){
response.send(200, "Hello World from Get Method");

Use the Mobile Service features

The Mobile service features like CRUD on tables or send push notification are available on the Api script.  The service are located in the request object. Indeed you can access to :

  • tables object using request.services.tables
  • push object using request.services.push

There is no other service in the request.services objects at this moment.

[Edit : 2013-06-13]

You also have access to the user objet (and get user id etc…) with :

  • var user = request.user;

Import Scripts of other Api

One cool thing is that we can use the mechanism of the export to defined some function in an API script and use it in another. This is not possible with the Table Script.

To check this, we can create a custom function in another api call testExternalFunction and we add a exportable function :

exports.myfunction = function(){
return "hello World from my function";

once this is done, you can use the require function to import the script :

exports.get = function(request, response){
//var scriptToImport = require("./{scriptToImport}");
//or var scriptToImport = require("../api/{scriptToImport}");
var scriptToImport = require("../api/testexternalfunction");
var result = scriptToImport .myfunction(); response.send(200, result);

This new feature allows you to centralize some function in a “shared” api script and allow you to write a more maintainable code.

Enjoy Sourire

This entry was posted in English, Microsoft, Non classé, Windows Azure Platform and tagged , , . Bookmark the permalink.

8 Responses to What’s new in Windows Azure Mobile Service : Api Script

  1. allprog says:

    Great post! Is it possible to call these scripts from the client as well?

  2. allprog says:

    I’m a little confused. I tried the script inclusion but it doesn’t seem to work. I created an API script with a function and tried to “require” it from a table script but it always failed with “cannot find module” error. Is it possible that only APIs can require each other?

  3. Johann says:

    Hey Jeremie

    I have tried this:

    exports.get = function(request, response){
    response.send(200, “Hello World from Get Method”);

    but I get an error:

    Unexpected character encountered while parsing value: H. Path ”, line 0, position 0.

    Do you have any idea why this area occurs?

    • Hello Johann,

      I’ve create a new API and just wrote the code you mention and when I call the api using Fiddler, I get no error.

      So I’ve questions about that :
      Did you tried to call this get method from another script? or from a client ?
      Have you copy/paste the code from the web site directly into the html editor ?

      And finally is this error is in your client ? or in the mobile service server?

      This error is a parsing error, so If you’ve got this error on the client using the SDK, what kind of call have you made? On your example the result object you have to define in the InvokeApiAsync() is just string, ie. InvokeApiAsync(“yourapiname”, HttpMethod.Get, null) should work. If you defined a more complexe object (with members) the parser try to interpret the string “Hello World from Get Method” and because it doesn’t find any { to start parsing the JSON, it throw an error

      Hope this will help you 🙂

  4. spothao says:

    Do you know how to use it on html/javascript ?
    I wan to update the database with the REST api

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s