Skip to content

Instantly share code, notes, and snippets.

@cognivator
Last active June 15, 2020 22:49
Show Gist options
  • Save cognivator/93edf8a1df88950e5be046812db0ba3c to your computer and use it in GitHub Desktop.
Save cognivator/93edf8a1df88950e5be046812db0ba3c to your computer and use it in GitHub Desktop.
Insomnia template for Salesforce REST API with Oauth password flow

Install

Import the accompanying insomnia_salesforceAPI.json into Insomnia to create a workspace with sample requests that,

  • get a bearer token using Salesforce built-in Oauth password workflow
  • make REST API requests referencing the bearer token

Setup / Customize

Environment

The environment variables are stored in MyOrg folder.

These environment values will be peculiar to your SF installation:

  • host: url of your Salesforce org
  • username: your Salesforce username
  • password: your Salesforce password
  • securityToken: your Salesforce security token
  • cId: Consumer Key of an Oauth-enabled Connected App in Salesforce
  • cSecret: Consumer Secret of an Oauth-enabled Connected App in Salesforce

These environment values are probably correct as-is, and will change only with SF releases:

  • ep: Salesforce API endpoint path
  • v: Salesforce API version
  • login_server: url of the built-in Salesforce auth token service for Oauth password flow

Bearer token reference

You may have to re-link the Bearer token of the sample GET request (see 'Get sobjects/account/describe' in the next section) to the response of the Oauth token request (see 'Get Bearer Token' in the next section). For details, refer to the Insomnia documentation.

Use

  1. Obtain an API bearer token by executing the first request, 'Get Bearer Token'
    • this request references the environment values to request a bearer token from the built-in Salesforce token service, using Oauth password flow
  2. Request a description of the Account sObject by executing the sample request, 'Get sobjects/account/describe'
    • this request references the bearer token obtained by 'Get Bearer Token'

To create additional API requests, duplicate 'Get sobjects/account/describe' and adjust the url / query parameters as necessary

Refs

Insomnia

Salesforce

{
"_type": "export",
"__export_format": 4,
"__export_date": "2020-06-15T21:37:56.788Z",
"__export_source": "insomnia.desktop.app:v7.1.1",
"resources": [
{
"_id": "req_a74cd1b5aeea4286b7be7cba3d0a6ed1",
"authentication": {},
"body": {},
"created": 1592256577872,
"description": "",
"headers": [],
"isPrivate": false,
"metaSortKey": -1592252582905.25,
"method": "POST",
"modified": 1592256745713,
"name": "Get Bearer Token",
"parameters": [
{ "description": "", "id": "pair_45a450e908db467690ecc8e68e3fbe0f", "name": "grant_type", "value": "password" },
{
"description": "",
"id": "pair_eadd99b8d99f4e15a34b85c965756453",
"multiline": false,
"name": "client_id",
"type": "text",
"value": "{{ cId }}"
},
{
"description": "",
"id": "pair_4e0a47e2291240ea9280b619b592e68c",
"name": "client_secret",
"value": "{{ cSecret }}"
},
{ "description": "", "id": "pair_89a0ad8a618948ca8912ef31cf4f3ed2", "name": "username", "value": "{{ un }}" },
{
"description": "",
"id": "pair_88a5099f1ca047209f9716b9bbe08b75",
"name": "password",
"value": "{{ pw }}{{ userSecret }}"
}
],
"parentId": "fld_430a1a015c0c43c8939f4a64c82717d4",
"settingDisableRenderRequestBody": false,
"settingEncodeUrl": true,
"settingFollowRedirects": "global",
"settingRebuildPath": true,
"settingSendCookies": true,
"settingStoreCookies": true,
"url": "{{ login_server }}",
"_type": "request"
},
{
"_id": "fld_430a1a015c0c43c8939f4a64c82717d4",
"created": 1592256735009,
"description": "",
"environment": {
"cId": "Consumer Key (of connected app)",
"cSecret": "Consumer Secret (of connected app)",
"ep": "services/data",
"host": "https://yourorg.salesforce.com",
"login_server": "https://login.salesforce.com/services/oauth2/token",
"pw": "password",
"un": "username",
"userSecret": "user security token",
"v": "v48.0"
},
"environmentPropertyOrder": {
"&": ["login_server", "host", "ep", "v", "un", "pw", "userSecret", "cId", "cSecret"]
},
"metaSortKey": -1592256735009,
"modified": 1592256918664,
"name": "MyOrg",
"parentId": "wrk_637eb0ce265243599235033197221d89",
"_type": "request_group"
},
{
"_id": "wrk_637eb0ce265243599235033197221d89",
"created": 1592256563657,
"description": "",
"modified": 1592256563657,
"name": "Insomnia - Salesforce API",
"parentId": null,
"_type": "workspace"
},
{
"_id": "req_31baaaf78926414ea83f2a017e7f20a3",
"authentication": {
"token": "{% response 'body', 'req_a74cd1b5aeea4286b7be7cba3d0a6ed1', 'b64::JC5hY2Nlc3NfdG9rZW4=::46b', 'never' %}",
"type": "bearer"
},
"body": {},
"created": 1592256588614,
"description": "",
"headers": [],
"isPrivate": false,
"metaSortKey": -1592252582855.25,
"method": "GET",
"modified": 1592256977141,
"name": "Get sobjects/account/describe",
"parameters": [],
"parentId": "fld_430a1a015c0c43c8939f4a64c82717d4",
"settingDisableRenderRequestBody": false,
"settingEncodeUrl": true,
"settingFollowRedirects": "global",
"settingRebuildPath": true,
"settingSendCookies": true,
"settingStoreCookies": true,
"url": "{{ host }}/{{ ep }}/{{ v }}/sobjects/account/describe",
"_type": "request"
},
{
"_id": "env_3ad5668ffcf63f24710bca87013655317474ccc7",
"color": null,
"created": 1592256563696,
"data": {},
"dataPropertyOrder": null,
"isPrivate": false,
"metaSortKey": 1592256563696,
"modified": 1592256563696,
"name": "Base Environment",
"parentId": "wrk_637eb0ce265243599235033197221d89",
"_type": "environment"
},
{
"_id": "jar_3ad5668ffcf63f24710bca87013655317474ccc7",
"cookies": [],
"created": 1592256563698,
"modified": 1592256563698,
"name": "Default Jar",
"parentId": "wrk_637eb0ce265243599235033197221d89",
"_type": "cookie_jar"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment