Getting Started with Flywheel SDK


The Flywheel SDK is a python toolbox that provides programmatic access to the Flywheel API endpoints.


Flywheel SDK has an MIT-based license.



The latest python package can be installed using pip:

pip install flywheel-sdk


The SDK requires an API key. You can find and generate your key on the Flywheel profile page. It will look like this:


Making API Calls

In order to make API calls, you will need to create an instance of the Flywheel client:

# import flywheel package
import flywheel

# Create client
fw = flywheel.Client('my-key')

Once you have a client instance, you can interact with the system. For instance, you could get information about yourself:

self = fw.get_current_user()
print('I am %s %s' % (self.firstname, self.lastname))

Using CLI Credentials

If you’ve logged in using the CLI, you can create a client instance without using an API key. This is useful when sharing SDK scripts for others to use.

# Create client, using CLI credentials
fw = flywheel.Client()

Finding Objects

With the exception of Groups, all containers and objects within Flywheel are referenced using Unique IDs. Groups are the only object that have a human-readable id (e.g. flywheel).

Finding an Object when you are only familiar with the label can be difficult. One method that may help is the resolve() method.

Resolve takes a path (by label) to an Object in the system, and if found, returns the full path to that Object, along with children. For example, to find the project labeled Anxiety Study that belongs to the flywheel group, I would call resolve with: 'flywheel/Anxiety Study':

# Resolve project by id
result = fw.resolve('flywheel/Anxiety Study')

# Extract the resolved project id
project = result.path[-1]

# Print the ids and labels of the path elements
for el in result.path:
        print('%s: %s' % (el.label,

# Print the children of project:
for el in result.children:
        print('%s: %s' % (el.label,

In a similar vein to resolve, lookup() will directly resolve a container by path. For example:

# Lookup project by id
project = fw.lookup('flywheel/Anxiety Study')

Finally, if the ID of the Object is known, then it can be retrieved directly using the flywheel.flywheel.Flywheel.get() method.

# Get session by id
session = fw.get('5bed87475b0ab53e50d03e0c')

Working with Objects

Most Objects in the Flywheel SDK provide methods for common operations. For example, to update properties on an object, you can simply call the update method, passing in a dictionary or key value pairs:

# Update a project's label
project.update(label='New Project Label')

# Update a subject's type and sex
subject.update({'type': 'human', 'sex': 'female'})

It’s important to note that calling update will not update your local copy of the object! However, you can quickly refresh an object by calling reload:

# Reload a session
session = session.reload()

Working with Finders

Another way to find objects is via Finders provided at the top level, and on objects. Finders allow locating objects via arbitrary filtering. Depending on which version of a finder method you call, you can retrieve all matching objects, or the first matching object. Finally, if you want to walk over a large number of objects, finders support iteration.

Filter strings are specified as the first argument to a find function. Multiple filters can be separated by commas. Filtering can generally be done on any property on an object, using dotted notation for sub-properties. Type conversion happens automatically. To treat a value as a string, wrap it in quotes: e.g. label="My Project".

Types supported are:

  • Dates in the format YYYY-MM-DD

  • Timestamps in the format YYYY-MM-DDTHH:mm:ss

  • Numeric values (e.g. 42 or 15.7)

  • The literal value null

Operations supported are:

  • Comparison operators: <, <=, =, !=, >=, >

  • Regular expression match: =~

In addition to filtering, sorting is supported in the sytax: <fieldname>:<ordering>. Where fieldname can be any property, and ordering is either asc or desc for ascending or descending order, respectively.

# Retrieve all projects (with a default limit)
all_projects = fw.projects()

# Find the first project with a label of 'My Project'
project = fw.projects.find_first('label=My Project');

# Find all sessions in project created after 2018-10-31
sessions = project.sessions.find('created>2018-10-31');

# Iterate over all failed jobs
for job in'state=failed'):
        print('Job: {}, Gear: {}'.format(,

# Iterate over all sessions belonging to project
for session in project.sessions.iter():

Dealing with Files

Often times you’ll find yourself wanting to upload or download file data to one of Flywheel’s containers. When uploading, you can either specify the path to the input file, or you can specify some in-memory data to upload using the FileSpec object.

# Upload the file at /tmp/hello.txt

# Upload the data 'Hello World!'
file_spec = flywheel.FileSpec('hello.txt', 'Hello World!\n', 'text/plain')

# Some endpoints allow multiple file uploads:
analysis.upload_output(['/tmp/hello1.txt', '/tmp/hello2.txt'])

When downloading, you specify the destination file, or you can download directly to memory

# Download file to /tmp/hello.txt
project.download_file('hello.txt', '/tmp/hello.txt')

# Download file contents directly to memory
data = project.read_file('hello.txt')

Occasionally you may want to see the contents of a zip file, and possibly download a single member without downloading the entire zipfile. There are a few operations provided to enable this. For example:

# Get information about a zip file
zip_info = acquisition.get_file_zip_info('')

# Download the first zip entry to /tmp/{entry_name}
entry_name = zip_info.members[0].path
out_path = os.path.join('/tmp', entry_name)
acquisition.download_file_zip_member('', entry_name, out_path)

# Read the "readme.txt" zip entry directly to memory
zip_data = acquisition.read_file_zip_member('', 'readme.txt')

Handling Exceptions

When an error is encountered while accessing an endpoint, an is thrown. The ApiException will typically have a status which is the HTTP Status Code (e.g. 404) and a reason (e.g. Not Found).

For example:

  project = fw.get_project('NON_EXISTENT_ID')
except flywheel.ApiException as e:
  print('API Error: %d -- %s' % (e.status, e.reason))

SSL CA Certificates

By default the SDK uses an internal set of CA certificates for SSL validation. If desired, this behavior can be overridden, and a set of PEM encoded certificates can be used instead.

To do this, set the FW_SSL_CERT_FILE to the absolute path of the certificates file.

For example:

export FW_SSL_CERT_FILE=/etc/ssl/cert.pem