Using SugarCRM REST Authentication with Postman

SugarCRM REST authentication does not fit in to any of the authentication options built in to Postman and this can be a barrier for teams that rely on Postman to explore REST APIs.

These steps will help get a working authentication setup for SugarCRM in Postman. Having a tool to run a quick test can speed up development as well as provide insights when troubleshooting. Treat this as a tool for those uses and not as a comprehensive solution or template for creation of an integration.

The script collects the token and adds it to the header for the request. It stores the time the token expires as an epoch timestamp and triggers a new token if that time is in the past.

Consider setting up an API platform as part of the process. Using "base" can invalidate the token for the browser session for your user.

API Platform Guide

  1. Create a new Collection
  2. Click on the Collection name
  3. Select the Variables tab
  4. Add the following variables
    • username
    • password
    • platform
    • sugarOAuthTokenUrl
    • sugarOAuthToken
    • sugarTokenExpires
  5. Fill in the Current Value for username
  6. Fill in the Current Value for password
  7. Fill in the Current Value for platform as "base" if you have not setup an API platform in Sugar
  8. Fill in the Current Value for sugarOAuthTokenUrl using the following format
    (Your base URL for SugarCRM )/rest/v11/oauth2/token
  9. Leave values for sugarOAuthToken and sugarTokenExpires blank
  10. Choose the Scripts tab
  11. Select Pre-request
  12. Paste in the script below and then Save the collection

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (!pm.collectionVariables.get("sugarOAuthToken") || pm.collectionVariables.get("sugarTokenExpires") < Date.now()) {
pm.sendRequest({
url: pm.collectionVariables.get("sugarOAuthTokenUrl"),
method: 'POST',
header: {
'Content-Type': 'application/json'
},
body: {
mode: 'raw',
raw: JSON.stringify({
grant_type: "password",
client_id: "sugar",
client_secret: "",
username: pm.collectionVariables.get("username"),
password: pm.collectionVariables.get("password"),
platform: pm.collectionVariables.get("platform")
})
}
}, (error, response) => {
if (error) {
console.log(error);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


It's time to try it out

  1. Add a GET request to the collection.
  2. Put in this URL:
    • ( Your base URL for SugarCRM )/rest/v11/Contacts/?max_num=10&offset=0&order_by=date_entered:desc&deleted=false&erased_fields=true&fields=id
  3. Add this header:
    • Content-Type : application/json
  4. Run the request to get the IDs for the 10 most recently created Contact records.
  5. A simple request and if that works it's time to start testing your ideas.

SugarCRM REST API Guide