Python wrapper for the Salesforce's Query API.
Project description
sfq
A lightweight Python library for querying Salesforce with minimal boilerplate.
Installation
pip install sfq
Quick Start
from sfq import SFAuth
sf = SFAuth(
instance_url="https://your-org.my.salesforce.com",
client_id="PlatformCLI",
client_secret="",
refresh_token="your-refresh-token"
)
# Query records
result = sf.query("SELECT Id, Name FROM Account LIMIT 5")
print(result["records"])
Getting Credentials
Use the Salesforce CLI to obtain authentication tokens:
# Login to your org
sf org login web --alias myorg --instance-url https://your-org.my.salesforce.com
# Display credentials
sf org display --target-org myorg --verbose --json
From the output, extract values from the sfdxAuthUrl field:
force://<client_id>:<client_secret>:<refresh_token>@<instance_url>
Core Features
Querying
Single Query
result = sf.query("SELECT Id, Name FROM Account LIMIT 10")
for record in result["records"]:
print(record["Name"])
Tooling API Query
result = sf.tooling_query("SELECT Id, FullName FROM CustomObject")
Batch Queries (cquery)
Execute multiple queries efficiently using the Composite API:
queries = {
"accounts": "SELECT Id, Name FROM Account LIMIT 5",
"contacts": "SELECT Id, Name FROM Contact LIMIT 5",
"users": "SELECT Id, Name FROM User LIMIT 5"
}
results = sf.cquery(queries)
for name, data in results.items():
print(f"{name}: {data['totalSize']} records")
CRUD Operations
Delete Records
result = sf.cdelete(["001xx000003DGbYAAW", "001xx000003DGbZAAW"])
# Returns: [{'id': '...', 'success': True, 'errors': []}, ...]
Static Resources
# Read by name
content = sf.read_static_resource_name("MyResource")
# Read by ID
content = sf.read_static_resource_id("081xx000003DGbYAAW")
# Update by name
sf.update_static_resource_name("MyResource", "<h1>New Content</h1>")
# Update by ID
sf.update_static_resource_id("081xx000003DGbYAAW", "<h1>New Content</h1>")
Platform Events
# List available events
events = sf.list_events()
# Publish single event
result = sf.publish("MyEvent__e", {"Message__c": "Hello!"})
# Publish batch
results = sf.publish_batch(
[{"Message__c": "Event 1"}, {"Message__c": "Event 2"}],
"MyEvent__e"
)
sObject Key Prefixes
# Get prefix -> object name mapping
prefixes = sf.get_sobject_prefixes()
# {'001': 'Account', '003': 'Contact', '005': 'User', ...}
# Get object name -> prefix mapping
prefixes = sf.get_sobject_prefixes(key_type="name")
# {'Account': '001', 'Contact': '003', 'User': '005', ...}
Limits API
limits = sf.limits()
print(limits["DailyApiRequests"]) # {'Max': 5000, 'Remaining': 4950}
Open Frontdoor URL
Open a browser session with valid authentication:
sf.open_frontdoor()
MDAPI Retrieve
Retrieve metadata components:
# Retrieve by type
result = sf.mdapi_retrieve(["CustomObject", "ApexClass"])
# Retrieve specific members
result = sf.mdapi_retrieve({
"CustomObject": ["Account", "Contact"],
"ApexClass": ["MyClass"]
})
HTML Table Output
result = sf.query("SELECT Id, Name FROM Account LIMIT 5")
html = sf.records_to_html_table(result["records"], styled=True)
Advanced Guides
- Authentication Guide - OAuth flows, token management, proxy configuration
- Platform Events Guide - Publishing and subscribing to events
- MDAPI Guide - Retrieving metadata components
- CI Integration Guide - CI/CD tracing and custom headers
- Telemetry Guide - Telemetry configuration and privacy
Exception Handling
from sfq import SFAuth, AuthenticationError, QueryError, APIError
try:
result = sf.query("SELECT Id FROM NonExistentObject")
except QueryError as e:
print(f"Query failed: {e}")
except APIError as e:
print(f"API error: {e}")
Available exceptions:
SFQException- Base exceptionAuthenticationError- Authentication failuresAPIError- General API errorsQueryError- Query-specific errorsQueryTimeoutError- Query timeout errorsCRUDError- CRUD operation errorsSOAPError- SOAP API errorsHTTPError- HTTP-level errorsConfigurationError- Configuration errors
Requirements
- Python 3.9+
- No external dependencies
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sfq-0.0.57.tar.gz.
File metadata
- Download URL: sfq-0.0.57.tar.gz
- Upload date:
- Size: 151.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32438002c5d2f29fe1589704244bb4436859c92e1b83784c642e6702f372b671
|
|
| MD5 |
1d13727163b63946feac96645037fa21
|
|
| BLAKE2b-256 |
07fd0af724e4c2e1822c14ae894f720d4329f70df30cd01bd6c92c477f96a0a8
|
Provenance
The following attestation bundles were made for sfq-0.0.57.tar.gz:
Publisher:
publish.yml on dmoruzzi/sfq
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sfq-0.0.57.tar.gz -
Subject digest:
32438002c5d2f29fe1589704244bb4436859c92e1b83784c642e6702f372b671 - Sigstore transparency entry: 947065414
- Sigstore integration time:
-
Permalink:
dmoruzzi/sfq@47a6c6fd7f73dc314b21ca9db94595149e267d25 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/dmoruzzi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@47a6c6fd7f73dc314b21ca9db94595149e267d25 -
Trigger Event:
push
-
Statement type:
File details
Details for the file sfq-0.0.57-py3-none-any.whl.
File metadata
- Download URL: sfq-0.0.57-py3-none-any.whl
- Upload date:
- Size: 57.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e924bd6ab55821cdeb77c2789427acb96a65cc3ceb6f7ed1e14f9b6c4a9f0b0
|
|
| MD5 |
4f7daea1069212d9815e1b0300718f37
|
|
| BLAKE2b-256 |
2eab2b564526fa2891f6fa4ae97a93fe6fabc97d3f79e4fe7a106b397d5a7767
|
Provenance
The following attestation bundles were made for sfq-0.0.57-py3-none-any.whl:
Publisher:
publish.yml on dmoruzzi/sfq
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sfq-0.0.57-py3-none-any.whl -
Subject digest:
3e924bd6ab55821cdeb77c2789427acb96a65cc3ceb6f7ed1e14f9b6c4a9f0b0 - Sigstore transparency entry: 947065415
- Sigstore integration time:
-
Permalink:
dmoruzzi/sfq@47a6c6fd7f73dc314b21ca9db94595149e267d25 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/dmoruzzi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@47a6c6fd7f73dc314b21ca9db94595149e267d25 -
Trigger Event:
push
-
Statement type: