API

API

SSMCM exposes almost all of its operations with its API.

The authentication is based on JWT with API keys; these keys can be managed from the user menu, in the option API keys.

The operations documentation is exposed using Swagger, in the URL /swagger/index.html, for example, https://www.meikai.cloud/swagger/index.html.

Examples

These examples use the requests Python library.

The first thing to do is to define some minimal variables: the base URL that will be prepended to every API URL, and the session, with some common parameters (in this case, the same header will be used in every request, so we create a session to not repeat it).

Python 3.6

These examples use Python 3.6; they can be adapted to another Python 3 version, replacing the use of f-strings.

base_url = "https://xxxxxx"
sess = requests.session()
sess.headers.update({"Accept": "application/json"})

For every request we will do, the response contains a code field, that represents the respose status. It should be always 200; in case of error, this code will be different, and a message about the error will be stored in the message field.

if res.get("code") and res.get("code") != 200:
    print(f"Error in request: {res.get('code')}: {res.get('message')}")

All responses (except export methods) return a JSON document, also with a code that we must check:

json_res = res.json()
if not json_res.get("code") or json_res.get("code") != 200:
    code = json_res.get("code") or "unknown"
    message = json_res.get("message") or "unknown"
    print(f"Error in request: {code}: {message}")

After that, we need to authenticate, using the /api/v1.0/auth/token. We need to pass a JSON dictionary with the API key we are using to authenticate. The response will contain a token, that we will use in the next requests.

res = sess.post(f"{base_url}/api/v1.0/auth/token", json={"key": key}).json()
token = res.get("data", {}).get("token")
sess.headers.update({"Authorization": f"Bearer {token}"})

Now we can use the rest of the API, using the methods documented in the API; for example, to get the list of S3 buckets:

print("Getting AWS S3 buckets...")
res = sess.get(f"{base_url}/api/v1.0/aws/s3?length=10")
# Remember to check for errors
print(res.json().get("data"))