[For introduction to PushCoin Web API, read Automation of tasks with PushCoin Web API]

One of the most common uses of the Web API is to synchronize PushCoin with the School’s Student Information System. For example, when a student leaves the school we want the POS system to notice it and lock the account. You probably want to schedule this as a nightly job, so the two systems remain in sync.

Secure File Transfer Protocol, or SFTP, is another option for uploading student records. You can read about it here.

First, we create a CSV file that contains the basic student information. The CSV file is typically generated by selecting rows of active students from the school database. A sample CSV file is provided here. Once the CSV file is ready (below called import.csv), we post it to PushCoin with:

Curl from shell (all on one line):

 curl "https://api.pushcoin.com/api/v1/guest/customers/file-csv"
 -k -X POST -F file=@import.csv -H "Authorization: Bearer <Access Key>"

In PowerShell (full example: PushCoin-UploadStudentData):

$uri = New-Object System.Uri("https://api.pushcoin.com/api/v1/guest/customers/file-csv")
$file = "C:\<filepath>\import.csv"
$client = New-Object System.Net.WebClient
$client.Headers.Add("Authorization", "Bearer <Access Key>")
$res = $client.UploadFile($uri, $file)
[System.Text.Encoding]::ASCII.GetString($res)

In Python:

import requests
r = requests.post('https://api.pushcoin.com/api/v1/guest/customers/file-csv',
  headers={ 'Authorization': 'Bearer <Access Key>'},
  files={'file': open('import.csv', 'rb')})

Remember: The CSV file must be submitted with content-type “multipart/form-data” according to RFC2388.

Next, we request a summary of the imported data to verify there were no errors (all on one line):

curl "https://api.pushcoin.com/api/v1/guest/customers/import/summary"
 -k -H "Authorization: Bearer <Access Key>"

which returns:

{
 "deleted_records": 0,
 "errors": 0,
 "new_records": 0,
 "updated_records": 0
 },
 ..
 }

If there were errors, you can retrieve them with:

curl "https://api.pushcoin.com/api/v1/guest/customers/import/errors"
 -k -H "Authorization: Bearer <Access Key>"

Finally, we commit all successfully imported records with:

curl "https://api.pushcoin.com/api/v1/guest/customers/import/commit"
 -k --data "{}" -X POST -H "Content-Type: application/json" -H "Authorization: Bearer <Access Key>"

Remember: You must commit student data, otherwise imported CSV-file will be discarded.

Tagged: