Simple-Salesforce
raw JSON → 1.12.9 verified Tue May 12 auth: no python install: verified
Simple-Salesforce is a basic Salesforce.com REST API client built for Python 3.9 and newer versions. It provides a low-level interface to the Salesforce REST Resource and APEX API, returning JSON responses as Python dictionaries. The library facilitates common operations like authentication, CRUD operations on Salesforce objects, SOQL/SOSL queries, and interaction with Bulk and Metadata APIs. It's actively maintained with regular bug fixes and feature enhancements.
pip install simple-salesforce Common errors
error ModuleNotFoundError: No module named 'simple_salesforce' ↓
cause This error occurs when the 'simple-salesforce' library is not installed, or the import statement uses an incorrect module name (e.g., 'salesforce' instead of 'simple_salesforce').
fix
Ensure the library is installed using
pip install simple-salesforce and imported as from simple_salesforce import Salesforce. error SalesforceAuthenticationFailed: invalid_grant: authentication failure ↓
cause This error means the provided Salesforce username, password, or security token is incorrect, or IP restrictions are preventing access.
fix
Verify all credentials are correct, ensure the security token is appended directly to the password if required, and check for any IP restrictions or login hour policies on the Salesforce side.
error SalesforceError: [StatusCode: 404] NOT_FOUND ↓
cause This indicates that the Salesforce object, field, or record you are trying to access or manipulate does not exist, or your user profile lacks permissions to see it.
fix
Double-check the API name of the object/field/record ID for typos, and ensure the authenticated user has appropriate read/write permissions for that resource in Salesforce.
error SalesforceError: [StatusCode: 500] INVALID_SESSION_ID: Session ID not found, please login again. ↓
cause The current Salesforce session has expired due to inactivity or invalidation, requiring re-authentication to obtain a new valid session.
fix
Re-establish the Salesforce connection by re-instantiating the
Salesforce object with your credentials to generate a fresh, valid session ID. error AttributeError: 'Salesforce' object has no attribute 'query_all' ↓
cause This error typically means you've misspelled a method name or an object's API name, or are attempting to access a non-existent attribute of the `Salesforce` connection object.
fix
Correct the method or object name to match
simple-salesforce's conventions or Salesforce's API names (e.g., sf.query_all() for queries or sf.Contact.get() for object methods). Warnings
breaking Breaking Change in v1.0.0: The default Salesforce API Version was increased to 42.0. This might affect queries or object interactions if your Salesforce instance expects an older API version. Additionally, support for Python 2.6, 2.7, 3.3, and 3.4 was removed. ↓
fix Explicitly specify the desired API version in the `Salesforce` constructor using `sf_version='X.Y'` (e.g., `sf_version='41.0'`). Ensure your project runs on Python 3.9+ for compatibility.
gotcha Security Token Confusion: When authenticating with username and password, a 'security token' is often required, especially from untrusted IP ranges. This token is usually emailed to you after a password reset and must be appended to the password or passed as a separate `security_token` argument. ↓
fix Obtain your Salesforce security token (often by resetting your password in Salesforce settings) and provide it either concatenated with your password or as the `security_token` parameter to the `Salesforce` constructor.
gotcha Bulk API 'InvalidBatch' Errors: When using the Bulk API (including Bulk 2.0), 'InvalidBatch' errors often indicate a schema mismatch, incorrect data types, or misrepresented fields in your payload. Error messages can be cryptic. ↓
fix Carefully review your data payload against the Salesforce object's schema, ensuring all fields exist, have correct data types, and required fields are present. Validate external IDs for upsert operations.
gotcha Connecting to a Salesforce Sandbox: To connect to a sandbox instance, you must explicitly specify `domain='test'` in the `Salesforce` constructor, in addition to your credentials. ↓
fix When initializing `Salesforce`, include `domain='test'` for sandbox environments (e.g., `sf = Salesforce(username=..., password=..., security_token=..., domain='test')`).
Install compatibility verified last tested: 2026-05-12
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.98s 57.6M
3.10 alpine (musl) - - 0.95s 56.6M
3.10 slim (glibc) wheel 4.5s 0.68s 58M
3.10 slim (glibc) - - 0.70s 57M
3.11 alpine (musl) wheel - 1.13s 60.8M
3.11 alpine (musl) - - 1.26s 59.7M
3.11 slim (glibc) wheel 4.3s 0.98s 61M
3.11 slim (glibc) - - 0.95s 60M
3.12 alpine (musl) wheel - 1.03s 52.5M
3.12 alpine (musl) - - 1.09s 51.4M
3.12 slim (glibc) wheel 3.7s 1.07s 53M
3.12 slim (glibc) - - 1.07s 52M
3.13 alpine (musl) wheel - 0.99s 52.2M
3.13 alpine (musl) - - 1.04s 51.0M
3.13 slim (glibc) wheel 3.7s 0.96s 53M
3.13 slim (glibc) - - 1.06s 51M
3.9 alpine (musl) wheel - 0.80s 57.6M
3.9 alpine (musl) - - 0.86s 56.6M
3.9 slim (glibc) wheel 5.2s 0.76s 58M
3.9 slim (glibc) - - 0.72s 57M
Imports
- Salesforce
from simple_salesforce import Salesforce - SalesforceLogin
from simple_salesforce.api import SalesforceLogin
Quickstart last tested: 2026-04-24
import os
from simple_salesforce import Salesforce
# It's highly recommended to use environment variables for credentials
USERNAME = os.environ.get('SF_USERNAME', 'your_username@example.com')
PASSWORD = os.environ.get('SF_PASSWORD', 'your_password')
SECURITY_TOKEN = os.environ.get('SF_SECURITY_TOKEN', 'your_security_token')
# Optional: for sandbox use domain='test'
# For specific API versions, use sf_version='X.Y'
try:
sf = Salesforce(
username=USERNAME,
password=PASSWORD,
security_token=SECURITY_TOKEN,
# domain='test' # Uncomment for sandbox
# sf_version='59.0' # Uncomment for a specific API version
)
print(f"Successfully connected to Salesforce instance: {sf.instance_url}")
# Example: Query Account records
query_result = sf.query("SELECT Id, Name FROM Account LIMIT 5")
print("\nFirst 5 Account Names:")
for record in query_result['records']:
print(f" - {record['Name']} (Id: {record['Id']})")
# Example: Create a new Account (replace with unique name for testing)
new_account_name = "Test Account from simple-salesforce_" + str(os.urandom(4).hex())
new_account = {'Name': new_account_name}
create_result = sf.Account.create(new_account)
print(f"\nCreated Account: {new_account_name} with Id: {create_result['id']}")
# Example: Delete the created Account (cleanup)
delete_result = sf.Account.delete(create_result['id'])
print(f"Deleted Account with Id: {create_result['id']}")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure your SF_USERNAME, SF_PASSWORD, and SF_SECURITY_TOKEN environment variables are set correctly.")