Quick Start Calling Admin API 2.x using Python
Overview
This is a quick start guide for calling Admin API using Python scripting, it will cover the basic operations of the Admin API:
Configure Environment with Python and Admin API 2.2
Once Admin API is installed, we can use Python versions above 3.7+. It is necessary to have the requests library installed or similar. We can use the following command to review our current python version.
python --version
The utility to install packages in Python is called pip, in case you don't have it installed you can follow the instructions in this link.
To install the libraries using pip, you can use the line below.
pip install -U requests
To import it into our script we will use the following imports:
script.py
import requests
import warnings
warnings.filterwarnings('ignore') # setting ignore as a parameter
Information
GET /
def information(base_url: str) -> dict:
'''
Retrieve API informational data
'''
endpoint = "/"
url = f"{base_url}{endpoint}"
r = requests.get(url, verify=False)
return r.json()
Our output should bring the information from the Restful API.
Sample Output
{
"version": "1.1",
"build": "1.0.0.0"
}
Authenticate to Admin API
In a new installation, it is necessary to previously register the client to connect, for which we will follow the instructions within the document in Securing Admin API.
Register a new client
In order to do so, we can add the functionality to our script by adding the following lines.
POST /connect/register
def register(
base_url: str,
client_payload: str,
) -> dict:
'''
Registers a new client
Parameters
----------
'base_url': base_url,
URL where API is hosted
client_payload: dict
The client information
{
'client_id': str,
The client id for the client
'client_secret': str,
The client secret for the client
display_name: str
Display name for client
}
'''
endpoint = "/connect/register"
url = f"{base_url}{endpoint}"
r = requests.post(
url,
data={
"ClientId": client_payload["client_id"],
"ClientSecret": client_payload["client_secret"],
"DisplayName": client_payload["display_name"],
},
verify=False
)
return r.json()
And we can construct our payload as the following example.
Sample input
new_client = {
'client_id': <your_client_id>,
'client_secret': <your_secret>,
'display_name': "Wille",
}
The successful output will be JSON formatted.
Sample output
{
"title": "Registered client 1 successfully.",
"status": 200
}
Token
Once we register our client according to the parameters specified in the document Securing Admin API.
We can obtain the token we will use for each API query. Just pass the same ClientID and ClientSecret we use to register it, with two new variables.
Sample input
client_id = <your_client_id>
client_secret = <your_secret>
grant_type = "client_credentials"
scope = "edfi_admin_api/full_access"
POST /connect/token
def token(
base_url: str,
client_id: str,
client_secret: str,
grant_type: str,
scope: str,
) -> dict:
'''
Retrieves a bearer token
Parameters
----------
base_url: str
URL where API is hosted
client_id: str
The client id provided in the register
client_secret: str
The client secret provided in the register
grant_type: str
default "client_credentials"
scope: str
default "edfi_admin_api/full_access"
'''
endpoint = "/connect/token"
url = f"{base_url}{endpoint}"
r = requests.post(
url,
data={
"client_id": client_id,
"client_secret": client_secret,
"grant_type": grant_type,
"scope": scope,
},
verify=False,
)
return r.json()
The outcome will be JSON formatted.
Sample output
{
"access_token": <your_token>,
"token_type": "Bearer",
"expires_in": 3599
}
Then you can use the token as an authentication method, with the header Authorization as the example below.
Vendors
Retrieve a Listing of Vendors
See the Endpoints - Admin API page for a complete list of resources and parameters. For this example, we will get a list of providers.
GET /v2/vendors
def get_vendors(
base_url: str,
access_token: str,
) -> dict:
'''
Retrieves all vendors
Parameters
----------
base_url: str
URL where API is hosted
access_token: str
String with the authorization token bearer
Returns
-------
r: List[Dict[str, str]]
Returns a list of dictionaries from the request
converted from JSON format.
[
{
"vendorId": 0,
"company": "string",
"namespacePrefixes": "string",
"contactName": "string",
"contactEmailAddress": "string",
}
]
'''
endpoint = "/v2/vendors?offset=0&limit=25"
url = f"{base_url}{endpoint}"
headers = {
'Authorization': f'Bearer {access_token}',
'Content-type': 'application/json',
'Accept': 'text/plain',
}
r = requests.get(
url=url,
headers=headers,
verify=False,
)
return r.json()
We will get a list of the vendors, JSON formatted, as in the example below.
Sample output
[
{
"id": 1,
"company": "ACME Education",
"namespacePrefixes": "ACME",
"contactName": "Wile E. Coyote",
"contactEmailAddress": "wile@acme.edu"
}
]
Create a Vendor
To create a new vendor, we will use the POST verb. Although in this example, it is necessary to pass a dictionary with the required data. Again, you can refer to the link Endpoints - Admin API to successfully create the provider. In our case, we will use the following information.
Sample output
vendor_payload = {
"company": "ACME Education",
"namespacePrefixes": "ACME",
"contactName": "Wile E. Coyote",
"contactEmailAddress": "wile@acme.edu",
}
Which we will pass as a parameter to a function as shown below, or with the method of your choice.
POST /v2/vendors
def create_vendor(
base_url: str,
access_token: str,
payload: dict,
) -> dict:
'''
Creates a vendor based on supplied values
Parameters
----------
base_url: str
URL where API is hosted
access_token: str
String with the authorization token bearer
payload: dict
{
"company": "string",
"namespacePrefixes": "string",
"contactName": "string",
"contactEmailAddress": "string",
}
'''
endpoint = "/v2/vendors"
url = f"{base_url}{endpoint}"
headers = {
'Authorization': f'Bearer {access_token}',
'Content-type': 'application/json',
'Accept': 'text/plain',
}
r = requests.post(
url=url,
headers=headers,
json=payload,
verify=False,
)
return r
As a result, we will obtain a 201 Status Code
Get a vendor
In the case that you want to retrieve information from one of the vendors, you will need to use the resource ID.
GET /v2/vendors/{id}
def get_vendor(
base_url: str,
access_token: str,
id: int,
) -> dict:
'''
Get an existing vendor using the resource identifier
Parameters
----------
base_url: str
URL where API is hosted
access_token: str
String with the authorization token bearer
id: int
Resource identifier
'''
endpoint = "/v2/vendors"
url = f"{base_url}{endpoint}/`{id}`"
headers = {
'Authorization': f'Bearer {access_token}',
'Content-type': 'application/json',
'Accept': 'text/plain',
}
r = requests.get(
url=url,
headers=headers,
verify=False,
)
return r.json()
In case of success we will obtain an output as follow:
Sample output
{
"Id": 9,
"company": "ACME Education",
"namespacePrefixes": "ACME",
"contactName": "Road Runner",
"contactEmailAddress": "roadrunner@acme.edu"
}
Update a vendor
For this example, we update the previously created vendor with the following info.
Sample input
vendor_payload = {
"company": "ACME Education",
"namespacePrefixes": "ACME",
"contactName": "Yosemite Sam",
"contactEmailAddress": "yosemitesam@acme.edu",
}
We use as an example the code below.
PUT /v2/vendors/{id}
def edit_vendor(
base_url: str,
access_token: str,
vendor_payload: dict,
id: int
) -> dict:
'''
Updates vendor based on the resource identifier
Parameters
----------
base_url: str
URL where API is hosted
access_token: str
String with the authorization token bearer
vendor_payload: dict
{
"company": "string",
"namespacePrefixes": "string",
"contactName": "string",
"contactEmailAddress": "string",
}
id: int
Resource identifier
'''
endpoint = "/v2/vendors"
url = f"{base_url}{endpoint}/`{id}`"
headers = {
'Authorization': f'Bearer {access_token}',
'Content-type': 'application/json',
'Accept': 'text/plain',
}
r = requests.put(
url=url,
headers=headers,
json=vendor_payload,
verify=False,
)
return r
As a result, we will obtain a 200 Status Code
Delete a vendor
To delete a vendor you can use the next point, as the example provided below.
/v2/vendors/{id}
def delete_vendor(
base_url: str,
access_token: str,
id: int,
) -> dict:
'''
Deletes an existing vendor using the resource identifier
Parameters
----------
base_url: str
URL where API is hosted
access_token: str
String with the authorization token bearer
id: int
Resource identifier
'''
endpoint = "/v2/vendors"
url = f"{base_url}{endpoint}/`{id}`"
headers = {
'Authorization': f'Bearer {access_token}',
'Content-type': 'application/json',
'Accept': 'text/plain',
}
r = requests.delete(
url=url,
headers=headers,
verify=False,
)
return r.json()
The output will be a confirmation as follows:
Sample output
{
"title": "Vendor deleted successfully"
}
Claim sets
List all Claims
To retrieve all the claims we will use the GET verb as follows:
GET /v2/claimsets
def get_claimsets(
base_url: str,
access_token: str,
) -> dict:
'''
Retrieves all claimsets
Parameters
----------
base_url: str
URL where API is hosted
access_token: str
String with the authorization token bearer
Returns
-------
r: List[Dict[str, str]]
Returns a list of dictionaries from the request
converted from JSON format.
[
{
"id": 0,
"name": "string",
"_isSystemReserved": false,
"_applications": [
{
"applicationName": "string"
}
]
}
]
'''
endpoint = "/v2/claimsets?offset=0&limit=25"
url = f"{base_url}{endpoint}"
headers = {
'Authorization': f'Bearer {access_token}',
'Content-type': 'application/json',
'Accept': 'text/plain',
}
r = requests.get(
url=url,
headers=headers,
verify=False,
)
return r.json()
The result will be a list of claim sets as the ones shown below:
Sample output
[
{
"id": 1,
"name": "AB Connect",
"_isSystemReserved": true,
"_applications": []
},
...
]