---
title: REST API and the Swimlane Python Driver Library
slug: user-guide-10x/rest-api-and-the-swimlane-python-driver-library
docTags: 
createdAt: 2025-06-18T11:01:37.461Z
---

﻿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:

:::BlockQuote
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(\*\*\{
&#x20;   'Event Name': 'demo security event 1',
&#x20;   'Event Message': 'this event is for demonstration purposes'
})
:::

 

:::BlockQuote
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:

:::BlockQuote
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:
&#x20;   if app\['name'] == 'Event':
&#x20;       app\_obj = app
&#x20;       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']:
&#x20;   if field\['name'] == 'Event Name':
&#x20;       name\_field\_id = field\['id']
&#x20;   if field\['name'] == 'Event Message':
&#x20;       msg\_field\_id = field\['id']

record = \{
&#x20;   'applicationId': app\_obj\['id'],
&#x20;   'values': \{
&#x20;       '$type': 'System.Collections.Generic.Dictionary\`2\[\[System.String, mscorlib],\[System.Object, mscorlib]], mscorlib',
&#x20;       name\_field\_id: 'demo security event 1',
&#x20;       msg\_field\_id: 'this event is for demonstration purposes'
&#x20;   }
}

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:

:::BlockQuote
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:
&#x20;   if app\['name'] == 'Event':
&#x20;       app\_obj = app
&#x20;       break

\# Find chosen field ID
msg\_field\_id = None
for field in app\_obj\['fields']:
&#x20;   if field\['name'] == 'Event Message':
&#x20;       msg\_field\_id = field\['id']
&#x20;       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:

:::hint{type="info"}
&#x20;The use of PATCH reduces the chances of harmful, colliding alterations to the record.
:::

:::BlockQuote
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:
&#x20;   if app\['name'] == 'Event':
&#x20;       app\_obj = app
&#x20;       break

\# Find chosen field IDs
name\_field\_id = None
msg\_field\_id = None
tracking\_field\_id = None
for field in app\_obj\['fields']:
&#x20;   if field\['name'] == 'Event Name':
&#x20;       name\_field\_id = field\['id']
&#x20;   if field\['name'] == 'Event Message':
&#x20;       msg\_field\_id = field\['id']
&#x20;   if field\['name'] == 'Tracking Id':
&#x20;       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.
:::

