Developer Guide
Rest API
REST API and the Swimlane Python Driver Library
5 min
the swimlane platform provides a rest api with numerous endpoints that allow developers to harness and extend the platform's automation and orchestration power as a developer, you can use any programming tool with an http library or capability you can also use the swimlane python driver https //swimlane python driver readthedocs io/en/stable/ documentation, which is a client library that can streamline your efforts to implement solutions in general, when authoring python scripts for swimlane, follow these guidelines when authoring scripts that will run outside of the platform, use the swimlane driver whenever possible and fall back on the unwrapped rest api when necessary tip see custom direct requests https //swimlane python driver readthedocs io/en/stable/examples/client html#custom direct requests from the swimlane python driver documentation to review the preferred method for blending these two approaches when authoring scripts that will run within swimlane, use the task api first, and then fall back on the driver and/or rest api when necessary swimlane python driver this is an example of a swimlane python driver script for connecting to swimlane and creating a new record in a specified application from swimlane import swimlane \# connect swimlane client host = 'host' base url = 'https //{0}' format(host) swimlane = swimlane(base url, 'user', 'password', verify ssl=false) \# retrieve app by name app = swimlane apps get(name='event') \# create new record new record = app records create( { 'event name' 'demo security event 1', 'event message' 'this event is for demonstration purposes' }) from swimlane import swimlane \# connect swimlane client host = 'host' base url = 'https //{0}' format(host) swimlane = swimlane(base url, 'username', 'password', verify ssl=false) \# retrieve app by name app = swimlane apps get(name='event') \# fetch existing record record = app records get(tracking id='ev1 4') \# modify record record\['event message'] = 'i\\'ve been updated!!!' \# persist the modification record patch() rest api the scripts above highlight the efficiency of scripting with swimlane python driver the examples that follow are example scripts in rest api here's a sample rest api script for connecting to swimlane and creating a new record in a specified application import requests from urlparse import urljoin s = requests session() \# authenticate host = 'host' base url = 'https //{0}' format(host) creds = {'username' 'username', 'password' 'password'} resp = s post(urljoin(base url, 'api/user/login'), json=creds) json content = resp json() token = json content pop('token', none) headers = {'authorization' 'bearer {}' format(token)} s headers update(headers) \# retrieve the chosen application resp = s get(urljoin(base url, 'api/app')) apps = resp json() app obj = none for app in apps if app\['name'] == 'event' app obj = app break \# create a new record in the chosen application \# first find pertinent field id values in order to make correct field value assignments name field id = none msg field id = none for field in app obj\['fields'] if field\['name'] == 'event name' name field id = field\['id'] if field\['name'] == 'event message' msg field id = field\['id'] record = { 'applicationid' app obj\['id'], 'values' { '$type' 'system collections generic dictionary`2\[\[system string, mscorlib],\[system object, mscorlib]], mscorlib', name field id 'demo security event 1', msg field id 'this event is for demonstration purposes' } } result = s post(urljoin(base url, 'api/app/' + app obj\['id'] + '/record'), json=record) print result here is a sample script for fetching an existing record, modifying it, and persisting the changes import requests from urlparse import urljoin import json s = requests session() \# authenticate host = 'host' base url = 'https //{0}' format(host) creds = {'username' 'user', 'password' 'password'} resp = s post(urljoin(base url, 'api/user/login'), json=creds) json content = resp json() token = json content pop('token', none) headers = {'authorization' 'bearer {}' format(token)} s headers update(headers) \# retrieve the chosen application resp = s get(urljoin(base url, 'api/app')) apps = resp json() app obj = none for app in apps if app\['name'] == 'event' app obj = app break \# find chosen field id msg field id = none for field in app obj\['fields'] if field\['name'] == 'event message' msg field id = field\['id'] break \# retrieve an existing record result = s get(urljoin(base url, 'api/app/' + app obj\['id'] + '/record/tracking/ev1 1')) \# print result rec = result json() \# print json dumps(rec, sort keys=true, indent=4, separators=(",", " ")) \# update the record rec\['values']\[msg field id] = 'i\\'ve been updated!!!' \# persist the alteration to the record result = s put(urljoin(base url, 'api/app/' + app obj\['id'] + '/record/' + rec\['id']), json=rec) print result here is the same script made more optimal by using http patch rather than http put to avoid rest api centric race conditions that can arise when two scripts are attempting to operate on the same record within the same span of time the use of patch reduces the chances of harmful, colliding alterations to the record import requests from urlparse import urljoin import json s = requests session() \# authenticate host = 'host' base url = 'https //{0}' format(host) creds = {'username' 'user', 'password' 'password'} resp = s post(urljoin(base url, 'api/user/login'), json=creds) json content = resp json() token = json content pop('token', none) headers = {'authorization' 'bearer {}' format(token)} s headers update(headers) \# retrieve the chosen application resp = s get(urljoin(base url, 'api/app')) apps = resp json() app obj = none for app in apps if app\['name'] == 'event' app obj = app break \# find chosen field ids name field id = none msg field id = none tracking field id = none for field in app obj\['fields'] if field\['name'] == 'event name' name field id = field\['id'] if field\['name'] == 'event message' msg field id = field\['id'] if field\['name'] == 'tracking id' tracking field id = field\['id'] \# retrieve an existing record result = s get(urljoin(base url, 'api/app/' + app obj\['id'] + '/record/tracking/ev1 1')) print(result) rec = result json() \# print json dumps(rec, sort keys=true, indent=4, separators=(",", " ")) \# update the record rec\['values']\[msg field id] = 'i\\'ve been updated!!!' rec\['values'] pop(tracking field id, none) # patch fails if tracking id present rec\['values'] pop(name field id, none) \# print json dumps(rec, sort keys=true, indent=4, separators=(",", " ")) \# persist the alteration to the record result = s patch(urljoin(base url, 'api/app/' + app obj\['id'] + '/record/' + rec\['id']), json=rec) print(result) \# print result text if you want to see the error message that accompanies an http 400 response