bg-gradient-blue
Dec, 4 2020

How to use an Application Token for API authentication?

blog

Each time you want to use the EMnify API, you need to authenticate. You can do so with your EMnify credentials or with an Application Token which is more secure. Here are the steps:

  1. Create an Application Token on the EUI;
  2. Generate an Authentication Token based on the application token;
  3. Use the Authentication Token for all other API

1. Create an Application token on the EMnify User interface 

  • Click on this symbol    in the right corner of the EUI.
  • Select "Create New Application Token"
  • Save the application token in a secure place 

2. Generate an Authentication Token from Postman

You can use the Application Token to create an Authentication Token in Postman, which you will then use to run any other EMnify API as explained here.

Once the application token is created, you can use it to generate an authentication token. Log in Postman, copy the application token as below and send the following API call. It will create an application token visible in the response body.

To automate authentication, you will have to create a variable with the authentication token from the response body. A new variable called: "Auth_Token" will be created. 

API Call

POST https://cdn.emnify.net/api/v1/authenticate

Headers

Content-Type: application/json

Body

First of all click on "Raw" :

insert the body below :  

{

"application_token":"daDayJhbasaDADGciOiJIUzUxMiJ9.eyJlc2MuYXBwc2VjcmV0IjoiMDgyMmU1MzgtM2NhYi00ZTI1LTlmOTgtZTA2NjU1MzBjYzRkIiwic3ViIjoic3VjY2Vzc0BlbW5pZnkuY29tIiwiYXVkIjoiXC9hcGlcL3YxXC9hcHBsaWNhdGlvbl90b2tlbiIsImVzYy5hcHAiOjczOSNjLnVzZXIiOjE5NDgwMiwiZXNjLm9yZyI6MTU3MiwiZXNjLm9yZ05hbWUiOiJFTW5pZnkgR3Jvd3RoIFRlYW0iLCJpc3MiOiJzcGMtZnJvbnRlbmQxMDFAc3BjLWZyb250ZW5kIiwiZXhwIjoxNjA5NDU1NTk5LCJpYXQiOjE1MTAwNTkyODl9IUHHTbioPOIsZSzar"

}

Tests: 

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("Auth_Token",jsonData.auth_token)

Response: status  200 OK

{

"auth_token":"UINiILhnHOIoiJIUzUxMiJ9.eyJhdWQiOiJcL2FwaVwvdjFcL2F1dGhlbnRpY2F0aW9uIiwiZXNjLmFwcCI6NzM5LCJlc2MudXNlciI6bnVsbCwiZXNjLm9yZyI6MTU3MiwiZXNjLm9yZ05hbWUiOiJFTW5pZnkgR554554454265FRlYW0iLCJpc3MiOiJzcGMtZnJvbnRlbmQxMDFAc3BjLWZyb250ZW5kIiwiZXhwIjoxNTEwMDczNzUzLCJpYXQiOjE1MTAwNTkzNTN9.nSAMOKzBeu33tS-ihdhuHiUIGFRTyGtTRFvytGF"

}

Note: Do not forget to save this request in order to refresh the authentication token for the following connections.

3. Use the Authentication Token for the following API requests

For demo purposes, we will integrate into the authentication token in the API call to GET the Endpoint Collection. The steps will be the following : 

  1. Send Authentication API with Application token to refresh authentication token (cf. above)
  2. Send the following API call to retrieve endpoint collection with the Auth token

API Call

GET https://cdn.emnify.net/api/v1/endpoint?page=1&per_page=20&sort=id

Headers

Content-Type: application/json

Authorization: Bearer

Response: status  200 OK

[

  {

    "id": 1,

    "name": "arduino01",

    "tags": "arduino, meter, temp",

    "created": "2014-08-01T08:47:00+00:00",

    "last_updated": "2016-02-29T14:02:47.000+0000",

    "status": {

      "id": 1,

      "description": "Disabled"

    },

    "service_profile": {

      "id": 1,

      "name": "Smart Meter"

    },

    "tariff_profile": {

      "id": 3,

      "name": "Domestic only"

    },

    "sim": {

      "id": 788,

      "iccid": "7368267365454773621",

      "imsi": "90199123565690",

      "msisdn": "+885637486456"

    },

    "imei": "8645454889321",

    "imei_lock": true,

    "ip_address": "10.288.23.75",

    "ip_address_space": {

      "id": 2

    },

]

From now on, you can use the EMnify API library. Do not forget to integrate the authentication token for each of your API calls.