Python wrapper for the Salesforce's Query API.
Project description
sfq (Salesforce Query)
sfq is a lightweight Python wrapper library designed to simplify querying Salesforce, reducing repetitive code for accessing Salesforce data.
For more varied workflows, consider using an alternative like Simple Salesforce. This library was even referenced on the Salesforce Developers Blog.
Features
- Simplified query execution for Salesforce instances.
- Integration with Salesforce authentication via refresh tokens.
- Option to interact with Salesforce Tooling API for more advanced queries.
Installation
You can install the sfq library using pip:
pip install sfq
Usage
Querying Salesforce Data
from sfq import SFAuth
# Initialize the SFAuth class with authentication details
sf = SFAuth(
instance_url="https://example-dev-ed.trailblaze.my.salesforce.com",
client_id="PlatformCLI",
refresh_token="your-refresh-token-here"
)
# Execute a query to fetch account records
print(sf.query("SELECT Id FROM Account LIMIT 5"))
# Execute a query to fetch Tooling API data
print(sf.query("SELECT Id, FullName, Metadata FROM SandboxSettings LIMIT 5", tooling=True))
Querying from Bash
You can easily incorporate this into ad-hoc bash scripts or commands:
python -c "from sfq import SFAuth; sf = SFAuth(instance_url='$instance_url', client_id='$client_id', refresh_token='$refresh_token'); print(sf.query('$query'))" | jq -r '.records[].Id'
How to Obtain Salesforce Tokens
To use the sfq library, you'll need a client ID and refresh token. The easiest way to obtain these is by using the Salesforce CLI:
Steps to Get Tokens
-
Install the Salesforce CLI:
Follow the instructions on the Salesforce CLI installation page. -
Authenticate with Salesforce:
Login to your Salesforce org using the following command:sf org login web --alias int --instance-url https://corpa--int.sandbox.my.salesforce.com
-
Display Org Details:
To get the client ID, refresh token, and instance URL, run:sf org display --target-org int --verbose --json
The output will look like this:
{ "status": 0, "result": { "id": "00Daa0000000000000", "apiVersion": "62.0", "accessToken": "your-access-token-here", "instanceUrl": "https://example-dev-ed.trailblaze.my.salesforce.com", "username": "user@example.com", "clientId": "PlatformCLI", "connectedStatus": "Connected", "sfdxAuthUrl": "force://PlatformCLI::your-refresh-token-here::https://example-dev-ed.trailblaze.my.salesforce.com", "alias": "int" } }
-
Extract and Use the Tokens:
ThesfdxAuthUrlis structured as:force://<client_id>::<refresh_token>::<instance_url>You can extract and use the tokens in a bash script like this:
query="SELECT Id FROM User WHERE IsActive = true AND Profile.Name = 'System Administrator'" sfdxAuthUrl=$(sf org display --target-org int --verbose --json | jq -r '.result.sfdxAuthUrl' | sed 's/force:\/\///') clientId=$(echo "$sfdxAuthUrl" | sed 's/::/\n/g' | sed -n '1p') refreshToken=$(echo "$sfdxAuthUrl" | sed 's/::/\n/g' | sed -n '2p') instanceUrl=$(echo "$sfdxAuthUrl" | sed 's/::/\n/g' | sed -n '3p') pip install sfq && python -c "from sfq import SFAuth; sf = SFAuth(instance_url='$instanceUrl', client_id='$clientId', refresh_token='$refreshToken'); print(sf.query('$query'))" | jq -r '.records[].Id'
Notes
- Authentication: Make sure your refresh token is kept secure, as it grants access to your Salesforce instance.
- Tooling API: You can set the
tooling=Trueargument in thequerymethod to access the Salesforce Tooling API for more advanced metadata queries.
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.1.tar.gz.
File metadata
- Download URL: sfq-0.0.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0bc53400cbf7858e71a3f71c60895d5f96a14c104f202cc6781be574f854a7e3
|
|
| MD5 |
0423de4826fd53154e95cc4a1b8967d1
|
|
| BLAKE2b-256 |
6d5eed4d5a643aafcd393cd54fc124fb1612f0f643cf80f23cde969c947285d1
|
Provenance
The following attestation bundles were made for sfq-0.0.1.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.1.tar.gz -
Subject digest:
0bc53400cbf7858e71a3f71c60895d5f96a14c104f202cc6781be574f854a7e3 - Sigstore transparency entry: 152661844
- Sigstore integration time:
-
Permalink:
dmoruzzi/sfq@2de9037892b45b0884938dfeb43213e5381a70fa -
Branch / Tag:
refs/tags/0.0.1 - Owner: https://github.com/dmoruzzi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2de9037892b45b0884938dfeb43213e5381a70fa -
Trigger Event:
release
-
Statement type:
File details
Details for the file sfq-0.0.1-py3-none-any.whl.
File metadata
- Download URL: sfq-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fadc2201ebcb1abe7b9e62dd075625e3d8d0e83070004cd47e522a6d05de312a
|
|
| MD5 |
e32337bbf5c1c9ab0fb888500ff76ea1
|
|
| BLAKE2b-256 |
5b6547615668fb0310b9bebc2e4ae6fb9f76785b398529a66b6ba62fcae80193
|
Provenance
The following attestation bundles were made for sfq-0.0.1-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.1-py3-none-any.whl -
Subject digest:
fadc2201ebcb1abe7b9e62dd075625e3d8d0e83070004cd47e522a6d05de312a - Sigstore transparency entry: 152661845
- Sigstore integration time:
-
Permalink:
dmoruzzi/sfq@2de9037892b45b0884938dfeb43213e5381a70fa -
Branch / Tag:
refs/tags/0.0.1 - Owner: https://github.com/dmoruzzi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2de9037892b45b0884938dfeb43213e5381a70fa -
Trigger Event:
release
-
Statement type: