Element451Integrations Guide

Sample Python Script

Updated Article

A basic sample Python script can be downloaded below. The example scripts below show how to import the api sample script and then use a function to fetch data. The full python file contains several examples, including passing the template directly instead of template GUID.

The sample file is provided as is with no guarantee about the functionality of the script or functions within it. It can be downloaded and modified to fit your institution, and we encourage you to fully test any script you create that uses it.

Element451API.pyAttachment, available from your Element451 team

Example - Export User Identifiers

Prints the all of the users from the fetched JSON data.

Note: User search is different from a full export in that there is a limited number of fields it can return. However, it can return up to 5,000 contacts, while an export API request can only handle 50 at a time.

python
import json
import requests
import Element451API as E451 

my_client = "client"
my_api = "api.451.io"
my_feature = "1234abc456efg"
my_segment = "client.segments.4291"
project = ["First Name"]

print(E451.api_user_search(my_client, my_api, my_feature, my_segment, project))

Example - Data User Export

Prints the all of the data from the fetched JSON data.

python
import json
import requests
import Element451API as E451 

my_client = "client"
my_api = "api.451.io"
my_feature = "1234abc456efg"
my_segment = "client.segments.4291"
my_template = "client.template.8571"

print(E451.api_data_request(my_client, my_api, my_feature, my_segment, my_template))

Example - Importing into a MySQL Database

python
import Element451API as E451 
import mysql.connector

my_client = "client"
my_api = "api.451.io"
my_feature = "1234abc456efg"
my_segment = "client.segments.4291"
my_template = "client.template.8571"

# Get Element Data
elementdata = E451.api_data_request(
  my_client,
  my_api,
  my_feature,
  my_segment,
  my_template,
  column_key="slug")
# Connect to mysql database.
mydb = mysql.connector.connect(
  host="host",
  port="port",
  user="user",
  password="password",
  database="database")

mycursor = mydb.cursor()
val = []
for x in elementdata["data"]:
studentID = x["user-elementid"]
studentName = x["user-first-name"]
studentTerm = x["user-education-term"]
studentMajor = x["user-education-prefered-major"]
mytuple = (studentID, studentName, studentTerm, studentMajor)
val.append(mytuple)


sql = (
"INSERT INTO training_data (Student_ID, First, Term, Major) VALUES (%s, %s, %s, %s)"
)
mycursor.executemany(sql, val)
mydb.commit()
print(mycursor.rowcount, "records inserted")

mycursor.close()
mydb.close()