Ping

This is the minimum valid request. It does not require any authentication.

curl -d '{"action": "ping"}' 'https://api.endk.dev' 

Output:

{"action":"ping","payload":null,"status_code":200,"message":"pong","request_id":"27IpBIn0AWdTS2iu3eXZOxIGNau"}

Redirect the output to a file:

curl -d '{"action": "ping"}' 'https://api.endk.dev' > output.json

This will create the file output.json (or overwrite it if it already exists).

View the file:

cat output.json

Output:

{"action":"ping","payload":null,"status_code":200,"message":"pong","request_id":"27InpNXjkS0yMFBHtKuTuKrBVvG"}

Pretty print the output:

python -m json.tool out.json

Output:

{
    "action": "ping",
    "payload": null,
    "status_code": 200,
    "message": "pong",
    "request_id": "27InpNXjkS0yMFBHtKuTuKrBVvG"
}

You can also use the jq command to print the JSON output with formatting and syntax highlighting:

jq < out.json

# or

cat out.json | jq

Output:

{
  "action": "ping",
  "payload": null,
  "status_code": 200,
  "message": "pong",
  "request_id": "27InpNXjkS0yMFBHtKuTuKrBVvG"
}
{
  "action": "ping",
  "payload": null,
  "status_code": 200,
  "message": "pong",
  "request_id": "27InpNXjkS0yMFBHtKuTuKrBVvG"
}

Admin

An admin user can perform any action. For the development server, the password xinolabs can be used with any e-mail address to create an admin session.

curl -d '{"action": "login", "payload": {"email": "admin@example.com", "password":"xinolabs"}}' 'https://api.endk.dev' | jq

Output:

{
  "action": "login",
  "payload": {
    "session_id": "27IqV6u3bavynVr3xAzx8ndGX1V",
    "user_role": "admin",
    "invalid_attempts": 0
  },
  "status_code": 200,
  "message": "OK",
  "request_id": "27IqVD2wxs0b4IEQSASeZbyjA6t"
}

The session_id returned in this payload is required to perform actions.

Create Client

The client is the primary customer, and must exist in the database before anything else can be done.

Note that all docs are here.

curl -d '{"action": "create_client", "session_id": "27IqV6u3bavynVr3xAzx8ndGX1V", "payload": {"name": "Awesome Co."}}' 'https://api.endk.dev' | jq

Output:

{
  "action": "create_client",
  "payload": {
    "uid": "27Ir8xkS1dLQBH8ikWDU7mg4mRz",
    "name": "Awesome Co."
  },
  "status_code": 200,
  "message": "OK",
  "request_id": "27Ir8ydsv3EIPJOnMR0yLFXefre"
}

Python

Using Python makes it a lot easier to perform repeititive actions, as well as work with the output.

Here’s the simplest Python version:

ping01.py
import requests

resp = requests.post('https://api.endk.dev', json={'action': 'ping'})
print(resp.content)

Run:

python3 ping01.py

Output:

b'{"action":"ping","payload":null,"status_code":200,"message":"pong","request_id":"27IrXN0KfnbWwh1iX8BjUquwgnf"}'

Small improvement:

ping02.py
import requests

url = 'https://api.endk.dev'

resp = requests.post(url, json={'action': 'ping'})
print(resp.content)

Here, we set the URL to a variable. We will often use the URL multiple times, and we don’t want to type it out each time.

Using Returned Data

In this example, we’ll log in as the admin user, then use the session ID to view the client list.

import requests
import json

url = 'https://api.endk.dev'

resp = requests.post(url, json={'action': 'login', 'payload': {'email': 'admin@example.com', 'password': 'xinolabs'}}) (1)
data = resp.json()

print('The response to the login:')
print(data)
# whitespace
print('\n')

print('The payload:')
print(data['payload'])
print('\n')

print('The session_id:')
print(data['payload']['session_id'])
print('\n')

session_id = data['payload']['session_id'] (2)
print(f'The session_id is: {session_id}')
print('\n')

resp = requests.post(url, json={'action': 'get_clients', 'session_id': session_id ,'payload': {'email': 'admin@example.com', 'password': 'xinolabs'}}) (3)
data = resp.json()

print('The response to the client listing:')
print(json.dumps(data, indent=4)) (4)
1 Notice that all request data goes into the payload section.
2 Assign the session_id to a variable.
3 Use the session_id to make a request.
4 Use json.dumps to print the JSON with formatting to make it easier to read.

Output:

The response to the login:
{'action': 'login', 'payload': {'session_id': '27ItqpZpHs0iPAlcsPIxe4owmEz', 'user_role': 'admin', 'invalid_attempts': 0}, 'status_code': 200, 'message': 'OK', 'request_id': '27ItquMPxa21UnTEqJK4jTvwpOv'}


The payload:
{'session_id': '27ItqpZpHs0iPAlcsPIxe4owmEz', 'user_role': 'admin', 'invalid_attempts': 0}


The session_id:
27ItqpZpHs0iPAlcsPIxe4owmEz


The session_id is: 27ItqpZpHs0iPAlcsPIxe4owmEz


The response to the client listing:
{
    "action": "get_clients",
    "payload": [
        {
            "uid": "27Ir8xkS1dLQBH8ikWDU7mg4mRz",
            "name": "Awesome Co."
        },
        {
            "uid": "27IoJXO8FmGeGIpMiqObq1yKm42",
            "name": "Monkey Test"
        }
    ],
    "status_code": 200,
    "message": "OK",
    "request_id": "27ItqtATQWM66IhrQoYBJTUG3WP"
}

Python Script to Read Any File

api.py
#!/usr/bin/env python3

import sys
import json

import requests

for filename in sys.argv[1:]:
    with open(filename) as f:
        data = json.load(f)
        r = requests.post('https://api.endk.dev/', json=data)
        print(json.dumps(r.json(), indent=4))