Skip to content

Instantly share code, notes, and snippets.

@mircast
Created November 26, 2018 15:14
Show Gist options
  • Save mircast/2ac5b8df23aa7c924e8a1192b763a3b7 to your computer and use it in GitHub Desktop.
Save mircast/2ac5b8df23aa7c924e8a1192b763a3b7 to your computer and use it in GitHub Desktop.
Onfido API samples
"""
This is sample worflow using the Onfido API with raw requests (i.e.
not using the Onfido python client). It is designed to demonstrate
how the bits fit together.
"""
from os import getenv
import requests
# get this from your settings / api section at onfido.com
API_TOKEN = getenv('ONFIDO_TEST_API_TOKEN')
assert API_TOKEN.startswith('test_'), "Please use a 'test_' API token."
def _headers(api_token):
"""Format the API HTTP headers."""
return {
'Authorization': 'Token token=%s' % api_token,
'Content-Type': 'application/json'
}
def _post(url, data, headers=_headers(API_TOKEN)):
"""POST to the API endpoint, and return JSON response."""
response = requests.post(url, json=data, headers=headers)
if response.status_code not in (200, 201):
print "Error POSTing to API:", response.json()['error']['message']
return response.json()
def create_contact(email):
"""Create a contact in the Onfido system."""
# test contact, taken from Onfido docs
url = 'https://api.onfido.com/v2/applicants'
data = {
"title": "Mr",
"first_name": "John",
"last_name": "Smith",
"gender": "male",
"dob": "2013-02-17",
"telephone": "02088909293",
"country": "GBR",
"addresses": [{
"building_number": "100",
"street": "Main Street",
"town": "London",
"postcode": "SW4 6EH",
"country": "GBR",
"start_date": "2013-08-10"
}],
# email must be unique, hence passing it in as arg
"email": email
}
return _post(url, json=data)
def create_check(contact_id, check_type='express'):
"""Create a new check for a contact."""
url = 'https://api.onfido.com/v2/applicants/%s/checks' % contact_id
data = {
"type": check_type,
"reports": [{"name": "identity"}]
}
return _post(url, json=data)
requests==2.11.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment