How to Connect to Salesforce Using SQLForce

Opening a SQLForce connection to Salesforce uses one of two patterns:

  • session = SQLForce.Session(“type”, “username”, “password”, [,”token”] [, proxyHost=”host:port” [, proxyUser=”user”, proxyPassword=”password”]])
  • session = SQLForce.Session(“ProfileName”)

The first form includes Salesforce credentials as part of the call, the second form looks up credentials in an external file.

In both cases, up to four parameters are required to establish a Salesforce session:

Parameter Required Description
 type Yes The type of Salesforce instance to connect to. Supported values are:

  • Production
  • Sandbox
  • DatabaseDotCom
username Yes Salesforce username to authenticate with. If the Salesforce type is Sandbox, remember to append the sandbox name.

If you use a custom domain your login host will not match one of the standard Salesforce domains. In this case, prepend your username with the name of your login host. For example:

  • mydomain.my.salesforce.com:myuser@demo.capstorm.com
password Yes Salesforce password to authenticate with.
token If Needed By Salesforce Security token required by Salesforce when connecting through the API. This value is required if your Salesforce instance mandates the use of security tokens.
proxyHost No Hostname and port used for Salesforce connections when connecting through a proxy. Examples:

  • 172.16.10.10:8013
  • myproxy.myco.com:7834
proxyUser No Username used to authenticate with the proxy. This parameter is only needed if your proxy requires authentication.
proxyPassword No Password used to authenticate with the proxy. This parameter is only needed if your proxy requires authentication.

Embedded Credentials

The simplest way to connect to Salesforce is to include Salesforce credentials in the call to SQLForce.session(). The basic form of a Session() call is:

  • session = SQLForce.Session(“type”, “username”, “password” [, “token”])

For example:

  • session = SQLForce.Session(“Sandbox”, “myuser@demo.capstorm.com”, “itisawinner” )
  • session = SQLForce.Session(“Sandbox”, “myuser@demo.capstorm.com“, “itisawinner” , proxyHost=”192.168.3.72:8888″)
  • session = SQLForce.Session(“Sandbox”, “myuser@demo.capstorm.com”, “itisawinner” , proxyHost=”caspian:8888″, proxyUser=”greg”, proxyPassword=”flipper”)
  • session = SQLForce.Session(“Production”, “mydomain.my.salesforce.com:myuser@demo.capstorm.com“, “itisawinner” )

External Credentials

SQLForce credentials can be stored in a file named “$HOME/sqlforce.ini”, and can be referenced as part of a SQLForce.Session() call. This is a common approach to avoid embedding credentials in Python scripts. A typical sqlforce.ini file is included below — the additional parameter, name, is the value used to create a Salesforce connection in SQLForce:

  • session = SQLForce.Session(“productionMain”)