SalesForce Rest API Example Using Python 3

This python Rest API tutorial help to Access SalesForce Rest API.The SalesForce API is use to access resources from across the micro services. The SalesForce REST API uses the same underlying data model and standard objects as those in SOAP API.

Salesforce is a customer relationship management solution that brings companies and customers together. It’s one integrated CRM platform that gives all your departments — including marketing, sales, commerce, and service — a single, shared view of every customer.

REST API provides a powerful way to access resources.Its advantages include ease of integration, convenient, simple and development.

I am using python 3.4+ and flask framework to create HTTP get call.I am using some dependencies to create post request.

How To Access Sales Force API

We will install following dependencies using pip.The pip is the package manager for python application.First, we will create 'salesforce-api' folder and change directory into it.

How To Get SalesForce API Token

Salesforce uses the OAuth protocol to allow users of applications to securely access data without having to reveal username and password credentials.

After successfully authenticating the connected app user with Salesforce, you’ll receive an access token which can be used to make authenticated REST API calls.

How To Enable CORS to Access Salesforce Resources

CORS stand for Cross-Origin Resource Sharing, That help to enables web browsers to request resources from origins other than their own(cross-origin).

You need to add origin serving the code to a CORS whitelist, that means you makes a request to the salesforce Rest API from whitelist origin server. Salesforce returns the origin in the Access-Control-Allow-Origin HTTP header, along with any additional CORS HTTP headers.

Otherwise, Salesforce returns HTTP status code 403.

Let’s install python dependencies using pip.

$sample-api> py -m pip install flask requests flask_cors flask_restful

We will create api.py file and added below code into this file –

from flask import Flask
from flask_restful import Resource, Api
from flask_cors import CORS
import requests
from flask import request
import json

app = Flask(__name__)
CORS(app) ## To allow direct AJAX calls

@app.route('/blog_post', methods=['GET'])
def get_services():

    r = requests.post('https://instance_name.salesforce.com/services/data/v47.0/', json = request.data)
    try:        
        headers = {'Authorization': 'Bearer sessionID', 'Content-Type': 'application/json'}
        url = "https://instance_name.salesforce.com/services/data/v47.0/";
        res = requests.get(url, headers=headers)

        return r.json()

   except Exception as e:
        print('unable to services from salesforce.')
        print(e.strerror)

if __name__ == '__main__':
   app.run(debug = True)

Escaping the Session ID or Using Single Quotes on Mac and Linux Systems, You can also escape special character from session string using back slash(/).