Using Azure CLI on Mac OS X to create Mobile Service

One of our startups at MS Ventures was looking to use the command line to manage their Azure account on Mac. If you prefer using Mac OS X Terminal or iTerm and have an Azure account then you should check out the Azure CLI tools for Mac.

Azure CLI

Install Azure CLI for Mac

You can either download the Azure Command Line tools installer for Mac or install the Azure command line tools using nodejs. I have previously written a post covering installing nodejs using NVM.

npm install -g azure-cli@0.8.17

Test Azure command works with help:

azure -h

Download Azure Publish Settings file

Sign in to your Azure account and download your *.publishsettings subscription file.

Import Publish Settings file

Import path to downloaded Azure Publish Settings file:

azure account import <file.publishsettings>

Tip: You can drag & drop the file into Terminal instead of typing the link.

Show Azure account subscriptions:

azure account list

Create Mobile Service with Azure CLI

Create Mobile Service TerminalTest:
azure mobile create TerminalTest --push "nh"

You will be prompted to enter SQL admin user name and password. The Mobile Service should only take a couple of minutes to provision.

Show Mobile Service details including applicationUrl and applicationKey:
azure mobile show TerminalTest

You will want to make a note of your applicationUrl and applicationKey for CRUD examples below.

Create Table Items:
azure mobile table create TerminalTest Items
Show Tables:
azure mobile table list TerminalTest
Read Table Items:
azure mobile data read TerminalTest Items

CRUD examples with Mobile Service using curl

You can quickly execute Create, Read, Update and Delete operations with Mobile Services using curl commands. Here’s a quick overview of Mobile Service CRUD operations and methods:

Operation Method REST URL format
Create / Insert POST https://<service_name>.azure-mobile.net/tables/<table_name>
Read / Query GET https://<service_name>.azure-mobile.net/tables/<table_name>
Update PATCH https://<service_name>.azure-mobile.net/tables/<table_name>/<item_id>
Delete DELETE https://<service_name>.azure-mobile.net/tables/<table_name>/<item_id>

You will need to replace the X-ZUMO-APPLICATION header value below with your own Mobile Service applicationKey.

  1. Create / Insert
     curl --request POST \
     --header 'X-ZUMO-APPLICATION: RLKPKSRTzFUFgXqVLRPkUTOKorqRQV65' \
     --header 'Content-Type: application/json; charset=UTF-8' \
     --data '{"text":"hello mac","complete":false}' \
     --location 'https://terminaltest.azure-mobile.net/tables/Items'
    

    Change the data text to submit a couple of items… Note you will get a JSON response with the id of the inserted item. This item id value is to be used in the Update and Delete examples below.

  2. Read / Query
     curl --request GET \
     --header 'X-ZUMO-APPLICATION: RLKPKSRTzFUFgXqVLRPkUTOKorqRQV65' \
     --header 'Content-Type: application/json; charset=UTF-8' \
     --location 'https://terminaltest.azure-mobile.net/tables/Items'
    
  3. Update
     curl --request PATCH \
     --header 'X-ZUMO-APPLICATION: RLKPKSRTzFUFgXqVLRPkUTOKorqRQV65' \
     --header 'Content-Type: application/json; charset=UTF-8' \
     --data '{"complete":true}' \
     --location 'https://terminaltest.azure-mobile.net/tables/Items/C9CCC7F7-4F16-4F63-8ACD-BD2DCC32F4CC'
    
    azure_hello_mac
    ^ Data updates shown in Azure Mobile Service table.
  4. Delete
     curl --request DELETE \
     --header 'X-ZUMO-APPLICATION: RLKPKSRTzFUFgXqVLRPkUTOKorqRQV65' \
     --header 'Content-Type: application/json; charset=UTF-8' \
     --location 'https://terminaltest.azure-mobile.net/tables/Items/C9CCC7F7-4F16-4F63-8ACD-BD2DCC32F4CC'
    

More documentation on Azure xplat-cli

How to query and filter Mobile Service table data with curl

Get items marked as complete

curl --request GET \
--header 'X-ZUMO-APPLICATION: RLKPKSRTzFUFgXqVLRPkUTOKorqRQV65' \
--header 'Content-Type: application/json; charset=UTF-8' \
--location "https://terminaltest.azure-mobile.net/tables/Items?\$filter=(complete%20eq%20true)"

Get items starting with text ‘hello’ with total count of results:

curl --request GET \
--header 'X-ZUMO-APPLICATION: RLKPKSRTzFUFgXqVLRPkUTOKorqRQV65' \
--header 'Content-Type: application/json; charset=UTF-8' \
--location "https://terminaltest.azure-mobile.net/tables/Items?\$filter=(startswith(text,'hello'))&\$inlinecount=allpages"

Get a collection of items in order (eg. pagination):

curl --request GET \
--header 'X-ZUMO-APPLICATION: RLKPKSRTzFUFgXqVLRPkUTOKorqRQV65' \
--header 'Content-Type: application/json; charset=UTF-8' \
--location "https://terminaltest.azure-mobile.net/tables/Items?\$orderby=text%20desc&\$skip=1&\$top=1"

NB: Just be aware if you use double quotes for --location then you must escape the dollar signs that prefix the params with a backslash (eg. \$). Single quotes don’t require dollar escaping but you may have trouble encapsulating filters with quoted strings hence I have opted for double quotes in these query examples.