A basic Salesforce.com REST API client for Python applications.
npx @tessl/cli install tessl/pypi-simple-salesforce@1.12.0A comprehensive Python library providing a basic REST API client for Salesforce.com. Simple-Salesforce offers unified access to Salesforce's REST API, Bulk API v1.0 and v2.0, and Metadata API through a clean, Pythonic interface. The library supports multiple authentication methods and provides both low-level and high-level operations for Salesforce data management.
pip install simple-salesforcefrom simple_salesforce import Salesforce, SalesforceLoginCommon for specific functionality:
from simple_salesforce import (
SFType, SFBulkHandler, format_soql, format_external_id
)Exception handling:
from simple_salesforce import (
SalesforceError, SalesforceAuthenticationFailed,
SalesforceExpiredSession, SalesforceResourceNotFound
)from simple_salesforce import Salesforce
# Password authentication
sf = Salesforce(
username='user@example.com',
password='mypassword',
security_token='mytoken'
)
# Query records
results = sf.query("SELECT Id, Name FROM Account LIMIT 10")
for record in results['records']:
print(f"Account: {record['Name']} (ID: {record['Id']})")
# Access specific SObject operations
account = sf.Account.get('001XX000003DHPr')
print(f"Account Name: {account['Name']}")
# Create new record
new_account = sf.Account.create({
'Name': 'Test Account',
'Type': 'Customer'
})
print(f"Created Account ID: {new_account['id']}")
# Bulk operations
bulk_data = [
{'Name': 'Bulk Account 1', 'Type': 'Customer'},
{'Name': 'Bulk Account 2', 'Type': 'Partner'}
]
bulk_results = sf.bulk.Account.insert(bulk_data)
# SOQL formatting with proper escaping
from simple_salesforce import format_soql
name = "O'Reilly Corp"
safe_query = format_soql("SELECT Id FROM Account WHERE Name = {}", name)
results = sf.query(safe_query)Simple-Salesforce provides a layered architecture that mirrors Salesforce's API structure:
This design enables both simple interactions for basic use cases and sophisticated bulk operations for enterprise scenarios, while maintaining consistent error handling and session management across all Salesforce APIs.
Multiple authentication methods supporting various Salesforce deployment scenarios including password authentication, OAuth 2.0 flows, JWT bearer tokens, and direct session access.
def SalesforceLogin(username=None, password=None, security_token=None, **kwargs): ...Core Salesforce class and SFType interface providing complete CRUD operations, queries, searches, and metadata access for Salesforce objects through the REST API.
class Salesforce:
def query(self, query, include_deleted=False, **kwargs): ...
def search(self, search): ...
def describe(self, **kwargs): ...
class SFType:
def create(self, data, headers=None): ...
def get(self, record_id, headers=None, **kwargs): ...
def update(self, record_id, data, **kwargs): ...
def delete(self, record_id, **kwargs): ...High-performance bulk operations for large-scale data manipulation using Salesforce's original Bulk API, supporting insert, update, upsert, delete, and query operations with batching.
class SFBulkHandler:
def submit_dml(self, object_name, dml, data, **kwargs): ...
class SFBulkType:
def insert(self, data, batch_size=10000, **kwargs): ...
def update(self, data, batch_size=10000, **kwargs): ...
def query(self, data, lazy_operation=False, **kwargs): ...Next-generation bulk operations with improved performance, simplified job management, and enhanced monitoring capabilities for modern high-volume data processing.
class SFBulk2Handler:
def create_job(self, operation, object_name=None, **kwargs): ...
def upload_job_data(self, job_id, data, **kwargs): ...
def wait_for_job(self, job_id, is_query, **kwargs): ...
def get_query_results(self, job_id, **kwargs): ...Complete metadata management including deployment, retrieval, and CRUD operations on Salesforce metadata components like custom objects, fields, workflows, and Apex classes.
class SfdcMetadataApi:
def deploy(self, zipfile, sandbox, **kwargs): ...
def retrieve(self, async_process_id, **kwargs): ...
def describe_metadata(self): ...
def list_metadata(self, queries): ...Formatting and utility functions for safe SOQL construction, external ID handling, data transformation, and error management across all Salesforce operations.
def format_soql(query, *args, **kwargs): ...
def format_external_id(field, value): ...
def quote_soql_value(value): ...Comprehensive exception hierarchy mapping HTTP status codes and Salesforce errors to specific Python exceptions for precise error handling and debugging.
class SalesforceError(Exception): ...
class SalesforceAuthenticationFailed(SalesforceError): ...
class SalesforceExpiredSession(SalesforceError): ...
class SalesforceResourceNotFound(SalesforceError): ...