Smartsheet Python SDK
The Smartsheet Python SDK is a library for connecting Python applications to Smartsheet services using API 2.0. It provides a programmatic interface to manage sheets, rows, columns, users, and other Smartsheet resources. The library simplifies interaction with the Smartsheet API, handling authentication, request serialization, and response deserialization. It is actively maintained, with the current version being 3.7.2, and typically sees updates for API endpoint changes and new features.
Warnings
- gotcha Authentication primarily relies on the `SMARTSHEET_ACCESS_TOKEN` environment variable for direct API access. For application development, OAuth 2.0 is supported but requires more setup. Misconfiguring or not setting the access token is a common source of 'Unauthorized' errors.
- gotcha By default, the SDK returns `Error` objects instead of raising exceptions for API errors. This can lead to silent failures if not handled correctly. Ensure you check the return type of API calls.
- breaking Smartsheet API endpoints are periodically updated or deprecated. Older SDK versions might not support new endpoints or parameters, leading to unexpected behavior or `AttributeError` if attempting to call non-existent methods.
- gotcha Many list operations (e.g., listing sheets, rows, contacts) are paginated by default. If you need all results, you must either explicitly pass `include_all=True` to the list method or implement manual pagination using `page` and `page_size` parameters.
- gotcha Accessing model creation methods (e.g., for Sheet, Column, Row objects) directly from `smartsheet.Smartsheet.models.ClassName` instead of using the methods on the instantiated service objects (e.g., `smart.Sheets.add_sheet()`) or correctly creating model instances, can lead to `AttributeError`.
Install
-
pip install smartsheet-python-sdk
Imports
- Smartsheet
import smartsheet smart = smartsheet.Smartsheet()
- models
import smartsheet sheet_obj = smartsheet.Smartsheet.models.Sheet()
Quickstart
import smartsheet
import os
# Ensure SMARTSHEET_ACCESS_TOKEN is set in your environment variables
# You can generate an access token in Smartsheet UI under Account > Personal Settings > API Access.
access_token = os.environ.get('SMARTSHEET_ACCESS_TOKEN', '')
if not access_token:
print("Error: SMARTSHEET_ACCESS_TOKEN environment variable not set.")
print("Please generate an access token from Smartsheet and set it.")
else:
try:
# Create a Smartsheet client
smart = smartsheet.Smartsheet(access_token)
# Optional: Enable exceptions for API errors instead of returning Error objects
smart.errors_as_exceptions(True)
# List all sheets the authenticated user has access to
response = smart.Sheets.list_sheets()
if response.data:
first_sheet = response.data[0]
print(f"Found sheet: {first_sheet.name} (ID: {first_sheet.id})")
# Get a specific sheet by ID
sheet = smart.Sheets.get_sheet(first_sheet.id)
print(f"The sheet '{sheet.name}' has {sheet.total_row_count} rows.")
else:
print("No sheets found for this user.")
except smartsheet.exceptions.SmartsheetException as e:
print(f"An error occurred: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")