Use a Config File to store User Credentials

This quick note shows a way of setting up a “config file” stored on your local machine to store user credentials for API calls. The example below shows how this could work for Salesforce credentials.

import pandas as pd
from simple_salesforce import Salesforce

Open Credentials

import os                                 # use os to navigate file structure
cwd = os.getcwd()                         # save the current working directory
os.chdir('/Users/ryanwingate/Documents/') # navigate to the directory where the config file is stored
import config                             # import the config file
sf = Salesforce(username=config.username,
os.chdir(cwd)                             # Restore the original working directory


desc = sf.describe()
objects = []
obj_labels = [obj['label']  for obj in desc['sobjects']]
obj_names  = [obj['name']   for obj in desc['sobjects']]
obj_custom = [obj['custom'] for obj in desc['sobjects']]
for label, name, custom in zip(obj_labels, obj_names, obj_custom):
    objects.append((label, name, custom))
objects = pd.DataFrame(objects,
                       columns = ['label','name','custom'])
print(str(objects.shape[0]) + ' objects')
464 objects

Example contents of

username=‘[email protected]