Salesforce.com REST API client built for Python

Simple Salesforce is a basic Salesforce.com REST API client built for Python 2.6, 2.7, 3.3 and 3.4. The goal is to provide a very low-level interface to the REST Resource and APEX API, returning a dictionary of the API JSON response.

You can find out more regarding the format of the results in the Official Salesforce.com REST API Documentation

There are two ways to gain access to Salesforce

The first is to simply pass the domain of your Salesforce instance and an access token straight to Salesforce()

For example:

from simple_salesforce import Salesforce
sf = Salesforce(instance=\'na1.salesforce.com\', session_id=\'\')

If you have the full URL of your instance (perhaps including the schema, as is included in the OAuth2 request process), you can pass that in instead using instance_url:

from simple_salesforce import Salesforce
sf = Salesforce(instance_url=\'https://na1.salesforce.com\', session_id=\'\')

There are also two means of authentication, one that uses username, password and security token and the other that uses IP filtering, username, password and organizationId

To login using the security token method, simply include the Salesforce method and pass in your Salesforce username, password and token (this is usually provided when you change your password):

from simple_salesforce import Salesforce
sf = Salesforce(username=\'[email protected]\', password=\'password\', security_token=\'token\')

To login using IP-whitelist Organization ID method, simply use your Salesforce username, password and organizationId:

from simple_salesforce import Salesforce
sf = Salesforce(password=\'password\', username=\'[email protected]\', organizationId=\'OrgId\')

If you\’d like to enter a sandbox, simply add sandbox=True to your Salesforce() call.

For example:

from simple_salesforce import Salesforce
sf = Salesforce(username=\'[email protected]\', password=\'password\', security_token=\'token\', sandbox=True)

Note that specifying if you want to use a sandbox is only necessary if you are using the built-in username/password/security token authentication and is used exclusively during the authentication step.

If you\’d like to keep track where your API calls are coming from, simply add client_id=\'My App\' to your Salesforce() call.

from simple_salesforce import Salesforce
sf = Salesforce(username=\'[email protected]\', password=\'password\', security_token=\'token\', sandbox=True, client_id=\'My App\')

ref Links:

https://developer.salesforce.com/blogs/developer-relations/2014/01/python-and-the-force-com-rest-api-simple-simple-salesforce-example.html

https://github.com/simple-salesforce/simple-salesforce

SFDC Bulk Api Python:

https://github.com/heroku/salesforce-bulk

https://plot.ly/python/salesforce/

https://python.swaroopch.com/

Open your pycharm editor and try below program to perform data management activities.

from simple_salesforce import Salesforce
import requests

session = requests.Session()
# manipulate the session instance (optional)
#Login Here
sf = Salesforce(
   username=\'xxxxxxxxx\', password=\'xxxxxx\', organizationId=\'xxxxxxxx\',session=session)
records =sf.query(\"SELECT Id, Name, Email FROM Contact where id = \'003o000000i4n5y\'\")
records = records[\'records\']

#print web page
print (\"Content-type: text/html\")
print (\"<html><body>\")
print (\"<h1>SELECT Id, Name, Email FROM Contact</h1>\")
print(\"<table>\")
print (\"<tr><td><b>Name</b></td><td><b>Email</b></td></tr>\")
for record in records:
   print (\"<tr><td>\"+record[\'Name\']+\"</td><td>\"+record[\'Email\']+\"</td></tr>\")
print (\"</table>\")

print (\"</body></html>\")

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome content in your inbox, every month.

We don’t spam! Please check your mail for confirmation.

Leave a Comment

Your email address will not be published. Required fields are marked *

Select Language »