Logging into the Magpie REST API
To use any other endpoints in the REST api, you will need to authenticate against the login endpoints which will provide you with a session token. The REST api allows for logging in with a username and password, a personal access token, or using single sign on (provided this feature has been implemented on your instance). The token will need to be retrieved from the returned JSON and either added as a cookie or as an auth header to subsequent requests.
Logging in using curl
# using password auth
curl -X POST https://<your_organization>.silect.is/api/magpie/login -H "Content-Type: application/json" -d '{"username": "auser@silect.is", "password": "P@ssW0rd!"}'
# returns {"token": "a token"}
# using access token auth
curl -X POST https://<your_organization>.silect.is/api/magpie/login -H "Content-Type: application/json" -d '{"token": "ABC123DEF456"}
# returns {"token": "a token"}
# using sso
curl -X POST https://<your_organization>.silect.is/api/magpie/login -H "Content-Type: application/json" -d '{"email": "user@silect.is"}
# returns {"token": "a token"}
# using the token as auth header
curl -X GET https://<your_organization>.silect.is/api/magpie/version -H "Authorization: token <the session token returned from login>"
# using the token as cookie
curl -X GET https://<your_organization>.silect.is/api/magpie/version -b "token=<the session token returned from login>"
Logging in using python
import requests
import json
base_url = "https://<your_organization>.silect.is/api/magpie/login"
headers = {"Content-Type": "application/json"}
# Using password auth
response = requests.post(
base_url,
headers=headers,
json={"username": "auser@silect.is", "password": "P@ssW0rd!"}
)
token = response.json()["token"]
print(f"Token: {token}")
# Using access token auth
response = requests.post(
base_url,
headers=headers,
json={"token": "ABC123DEF456"}
)
token = response.json()["token"]
print(f"Token: {token}")
# Using SSO
response = requests.post(
base_url,
headers=headers,
json={"email": "user@silect.is"}
)
token = response.json()["token"]
print(f"Token: {token}")
# Using the token as auth header
headers = {"Authorization": f"token {token}"}
response = requests.get(
"https://<your_organization>.silect.is/api/magpie/version",
headers=headers
)
# Using the token as cookie
cookies = {"token": token}
response = requests.get(
"https://<your_organization>.silect.is/api/magpie/version",
cookies=cookies
)