Skip to content

even-simpler-salesforce

The Salesforce client your web app deserves. Async or sync, same API.

import even_simpler_salesforce as esf

auth = esf.ClientCredentials(
    domain="https://acme.my.salesforce.com",
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
)

# Logs in on the first call. Refreshes the token for you.
async with esf.AsyncSalesforce(auth) as sf:
    # Values are quoted for you: no SOQL injection.
    lead = await sf.query_one(
        "SELECT Id, Name FROM Lead WHERE Email = {}", email
    )

    try:
        await sf.sobject("Lead").update(lead["Id"], {"Status": "Working"})
    except esf.ValidationRuleFailed as exc:
        print(exc.message, exc.fields)  # one class per Salesforce error
import even_simpler_salesforce as esf

auth = esf.ClientCredentials(
    domain="https://acme.my.salesforce.com",
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
)

# Logs in on the first call. Refreshes the token for you.
with esf.Salesforce(auth) as sf:
    # Values are quoted for you: no SOQL injection.
    lead = sf.query_one(
        "SELECT Id, Name FROM Lead WHERE Email = {}", email
    )

    try:
        sf.sobject("Lead").update(lead["Id"], {"Status": "Working"})
    except esf.ValidationRuleFailed as exc:
        print(exc.message, exc.fields)  # one class per Salesforce error

even-simpler-salesforce

Beyond simple-salesforce

The parts you no longer write yourself

Six things simple-salesforce leaves to you. Here they come built in.

Async and sync One API. Await it in FastAPI, call it from Flask or a script.
async with esf.AsyncSalesforce(auth) as sf:
    await sf.query(soql)

with esf.Salesforce(auth) as sf:
    sf.query(soql)
simple-salesforce Sync only
Token refresh An expired token is refreshed once, and the call is sent again.
# The token expired an hour ago.
await sf.query("SELECT Id FROM Lead")
# 401 -> refreshed -> sent again -> rows
simple-salesforce Raises SalesforceExpiredSession
Typed errors One exception class per Salesforce error code, with the fields that failed.
except esf.ValidationRuleFailed as exc:
    print(exc.fields)  # ('Company',)
except esf.RowLocked:
    retry_later()
simple-salesforce One class per HTTP status
API budget Every app on the org shares one daily API limit. Pause batch jobs first, so live traffic keeps working.
budget = esf.Budget()
sf = esf.AsyncSalesforce(auth, budget=budget)
job = sf.tagged(priority="low")
# above 80% of the daily limit, job calls
# raise BudgetExceeded. Live calls still run.
simple-salesforce Reads usage, never stops a call
Readable logs One event per call, secrets redacted. Works with structlog and OpenTelemetry.
$ ESF_DEBUG=1 python sync.py
esf GET   query          Lead 200 115ms
esf PATCH sobject.update Lead 400  88ms
esf POST  oauth.refresh  rotated  240ms
simple-salesforce No structured events
Test fakes An in-memory org as a pytest fixture. No network, no mocks to write.
def test_close(esf_fake):
    lead = esf_fake.add("Lead", record)
    close_lead(esf_fake, lead)
    assert esf_fake.calls_for("sobject.update")
simple-salesforce No test helpers
Moving over? One command rewrites the mechanical part of your code. esf migrate src/

Beta (0.5)

Breaking changes are still possible in 0.x releases. See the project status for what is covered and where it has been tested.