Salesforce API Wrapper

0.1.46 · active · verified Thu Apr 16

The `salesforce-api` library (felixlindstrom/python-salesforce-api) provides an easy-to-use, flexible, and testable solution for communicating with Salesforce through its REST and SOAP APIs. It handles authentication, record management (CRUD), SOQL queries, and bulk operations. The library is currently at version 0.1.46 and is actively maintained with contributions as recent as 2024.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to Salesforce using username, password, and security token authentication. It then performs a simple SOQL query to retrieve account records. Always use environment variables or a secure configuration management system for credentials in production. You may need to create a Connected App in Salesforce and use OAuth for more robust authentication flows.

import os
from salesforce_api import Salesforce

# It's recommended to use environment variables for sensitive credentials.
SF_USERNAME = os.environ.get('SF_USERNAME', 'your_username@example.com')
SF_PASSWORD = os.environ.get('SF_PASSWORD', 'your_password')
SF_SECURITY_TOKEN = os.environ.get('SF_SECURITY_TOKEN', 'your_security_token')

# For sandbox environments, you might need to specify the domain.
# For example, domain='test'

try:
    client = Salesforce(
        username=SF_USERNAME,
        password=SF_PASSWORD,
        security_token=SF_SECURITY_TOKEN
    )
    print("Successfully connected to Salesforce!")

    # Example: Query an SObject
    # Note: Replace 'Account' and field names with actual objects/fields in your org.
    accounts = client.query("SELECT Id, Name FROM Account LIMIT 5")
    print("First 5 Accounts:")
    for record in accounts['records']:
        print(f"  ID: {record['Id']}, Name: {record['Name']}")

    # Example: Create a new contact (adjust fields as per your Salesforce org)
    # new_contact_data = {
    #     'LastName': 'TestContact',
    #     'Email': 'test@example.com'
    # }
    # result = client.Contact.create(new_contact_data)
    # print(f"Created Contact ID: {result['id']}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →