2. Create a session

Endpoint: Create Session

POST /api/sessions

This endpoint is used to create a new session for a device using a unique identifier. The response includes a token that can be used for subsequent authenticated requests.

Request

  • Endpoint: /api/sessions

  • Method: POST

  • Content-Type: application/json

Request Body

The request body should be a JSON object containing the following field:

{
  "identifier": "device_unique_id_here"
}

Fields:

  • identifier: string (Required) - The unique identifier associated with the device or session. This could be a device ID or any unique identifier that the system uses to track sessions.

Example Request

POST /api/sessions
Content-Type: application/json

{
  "identifier": "device_unique_id_here"
}

Response

  • 200 OK: The request was successful, and a session token is returned in the response body.

    • Response Body:

      {
        "data": {
          "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uIjoiNjJmY2ZlM2Y2ZTBiZGEyC"
        }
      }

Node.js (Axios) Example Code

const axios = require('axios');

let data = JSON.stringify({
  "identifier": "device_unique_id_here"
});

let config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'http://localhost:3002/api/sessions',
  headers: { 
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_ACCESS_TOKEN'
  },
  data : data
};

axios.request(config)
.then((response) => {
  console.log(JSON.stringify(response.data));
})
.catch((error) => {
  console.log(error);
});

Response Details

  • token: string - The session token that will be used for authenticating future API requests.

Use Cases

  • Session Management: Use this endpoint to create a new session for a device or user, which can then be used to authenticate subsequent API requests.

  • Token-Based Authentication: The token provided in the response can be included in the Authorization header for other API requests to authenticate the user or device.

Error Handling

  • 400 Bad Request: Returned if the identifier is missing or invalid.

  • 500 Internal Server Error: Returned if there is an issue on the server side while processing the request.

Example Response

{
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uIjoiNjJmY2ZlM2Y2ZTBiZGEyC"
  }
}

Last updated