Quickstart#
This quickstart shows the fastest path to:
Import
salespyforceInstantiate a
SalesforceclientRun a SOQL query
Perform a basic record operation
For setup and environment requirements, see Overview and Installation.
1. Import The Package#
import salespyforce
from salespyforce import Salesforce
print(salespyforce.__version__)
2. Instantiate A Client#
Use a helper file for local development and automation so credentials stay out of your source code.
from salespyforce import Salesforce
sfdc = Salesforce(helper="/path/to/helper.yml")
print(f"Connected to {sfdc.instance_url} using API {sfdc.version}")
You can also pass credentials directly:
from salespyforce import Salesforce
sfdc = Salesforce(
username="admin.user@example.com",
password="example123",
org_id="4DJ000000CeMFYA0",
base_url="https://example-dev-ed.lightning.force.com/",
endpoint_url="https://example-dev-ed.my.salesforce.com/services/oauth2/token",
client_id="3MVG9gTv.DiE8cKRIpEtSN_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
client_secret="7536F4A7865559XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
security_token="2muXaXXXXXXXXXXXXXXXoVKxz",
)
Tip
Need help choosing the authentication pattern? See Authentication Guide.
3. Run A SOQL Query#
Use soql_query() to fetch records from Salesforce:
query = """
SELECT Id, Name
FROM Account
ORDER BY LastModifiedDate DESC
LIMIT 5
"""
result = sfdc.soql_query(query)
print(f"Total rows returned: {result['totalSize']}")
for row in result.get("records", []):
print(row["Id"], row.get("Name"))
4. Perform A Record Operation#
Create a record with create_sobject_record():
payload = {"Name": "Acme - Quickstart Demo"}
create_result = sfdc.create_sobject_record("Account", payload)
if create_result.get("success"):
account_id = create_result["id"]
print(f"Created Account: {account_id}")
Optionally update the same record:
update_payload = {"Description": "Updated by salespyforce quickstart"}
sfdc.update_sobject_record("Account", account_id, update_payload)
print("Account updated.")
Where To Go Next#
Authentication details and helper-file formats: Authentication Guide
Query patterns and additional examples: Querying Guide
API errors, exceptions, and diagnostics: Error Handling Guide
Client and method reference: Client API Reference