Many, many, many moons ago I posted about writing an Insanely Simple Python Script that used the Salesforce REST API’s.  It was a great little example, updated later by Mr. Pat Patterson and eventually evolved into a longer script that I used for demonstrations.  Due to bad source control and a new laptop build, I recently lost the code to said longer script – but I still wanted a similar demo.  While losing code is a bummer, I always say that when life throws you lost code you hand life back new refactored code.

And so I recently discovered simple-salesforce, a Python REST API client for Salesforce which uses more moderns libs like requests to get a similar job done.  Out of the box, simple-salesforce is setup well for headless access to the REST API via a username and password pair:

Which is going to cover a lot of different use cases one might need. Once you are logged in, you can then access the REST API in a very object friendly manner. For instance, if I wanted to change the name of a custom object – I could:

There are convenience methods for all of the standard REST functions. The only problem with this demo is that I often show code to large groups of people at one time and really don’t want to be typing my super-secret passwords onto a big screen. My previous script used an OAuth work around that launched a browser and waited for user input – but for this iteration I wanted to show a proper OAuth flow. Since simple-salesforce will also accept an instance url and session ID, this wasn’t too hard:

Which is also available as a gist.  You can save this script as “”, serve it up on a webserver  and go to “/” to kick things off.  The Python script will consume the request token and get a proper access token in return.  From there I can use simple-salesforce to access the REST API.  If you wanted to try something like this out on OSX, it turns out that it is very easy to add Python scripts to Apache once you know where to look.

There you go, a quick and easy way to the use the REST API via Python.  If you’re using Python with Salesforce currently, or might in the future, let me know via twitter or in the comment boxes below.

If you like simple-salesforce, give Nick Catalano a shoutout on twitter.  You can also check out his OAuth2 implementation for Salesforce and Python.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS