What is Rest API?

What is Composite Rest API?

Why Composite Resources

Types of Composite Resources

Hey folks, In this blog, I will cover what is the Salesforce Composite Rest API, How we can use the Composite Rest API, and what the benefits are of Composite Rest API. 

What is Rest API?

A Rest API stands for RESTful API. Rest API is a web interface. It allows us to interact with a web application/platform. It allows us to access data without using the User interface. We can exchange data. We can perform operations. 

Rest API is used to integrate with other web applications. 70% of public APIs are REST APIs. REST APIs offer more flexibility, a gentler learning curve, and work directly from an HTTP URL.

Salesforce supports Rest API integrations. Using Rest API we can perform DML operations in the Salesforce platform by sending HTTP requests to endpoints in Salesforce. REST API is great for accessing and querying records. Salesforce Rest API is a great tool to query, create, modify or delete data. Following types of APIs are available in Salesforce.

  • Bulk 2.0 API
  • Metadata API
  • Connect REST API

 

Salesforce Rest API Example

URL Endpoint: https://yourInstance.salesforce.com/services/data/v53.0/sobjects/Merchandise__c/MerchandiseExtID__c/123 

 

Request Type: Get

 

Header: 

  • Authorization: Bearer token
  • Content-Type: application/json

 

 

Response:

 

{

    “attributes”: {

        “type”: “Account”,

        “url”: “/services/data/v53.0/sobjects/Account/0010I00001qmyqJQAQ”

    },

    “Id”: “0010I00001qmyqJQAQ”,

    “IsDeleted”: false,

    “MasterRecordId”: null,

    “Name”: “Wrapture Bulk Company”,

    “Type”: null,

    “RecordTypeId”: “012280000006nCTAAY”,

    “ParentId”: null,

    “BillingStreet”: null,

    “BillingCity”: null,

    “BillingState”: null,

    “BillingPostalCode”: null,

    “BillingCountry”: null,

    “BillingLatitude”: null,

    “BillingLongitude”: null,

    “BillingGeocodeAccuracy”: null,

    “BillingAddress”: null,

    “ShippingStreet”: null,

    “ShippingCity”: null,

    “ShippingState”: null,

    “ShippingPostalCode”: null,

    “ShippingCountry”: null,

    “ShippingLatitude”: null,

    “ShippingLongitude”: null,

    “ShippingGeocodeAccuracy”: null,

    “ShippingAddress”: null,

    “Phone”: null,

    “Fax”: null,

    “AccountNumber”: null,

    “Website”: null,

    “PhotoUrl”: “/services/images/photo/0010I00001qmyqJQAQ”,

    “Sic”: null,

    “Industry”: null,

    “AnnualRevenue”: null,

    “NumberOfEmployees”: null,

    “Ownership”: null,

    “TickerSymbol”: null,

    “Description”: null,

    “Rating”: null,

    “Site”: null,

    “OwnerId”: “005280000027NBrAAM”,

    “CreatedDate”: “2018-05-15T11:53:40.000+0000”,

    “CreatedById”: “005280000027NBrAAM”,

    “LastModifiedDate”: “2018-05-15T11:53:40.000+0000”,

    “LastModifiedById”: “005280000027NBrAAM”,

    “SystemModstamp”: “2018-05-15T12:02:56.000+0000”,

    “LastActivityDate”: null,

    “LastViewedDate”: “2021-12-09T09:38:36.000+0000”,

    “LastReferencedDate”: “2021-12-09T09:38:36.000+0000”,

   }

 

 

What is Composite Rest API?

Composite Rest API is an enhanced form of the Rest API which can execute a series of REST API requests in a single call. It’s like we can perform a set of operations in a single Rest API call. It is a way we can combine all the related operations in a single call.We can use the first request output to subsequent request parameter input. 

For example, let’s work in a Parent child relationship scenario where we need to create account records and it’s child contact records. Using only Rest API we will need to make 2 API callouts to create an Account and it’s child contacts, but using composite REST API we can complete this job in a single call. It technically reduces our API usage which is important for working in the Salesforce platform, as Salesforce has governor limitations for resource usages. All the series of Rest APIs are counted as a single call towards your API limits. This is executed in the context of the same user.

Composite Rest API is Available in API version 43.0 and later. We can have up to 25 subrequests in a single call. Up to 5 of these subrequests can be sObject Collections or query operations, including Query and QueryAll requests.

Composite is supported for the following resources.

  • All sObject resources (vXX.X/sobjects/), including sObject Rows by External ID
  • The Query resource (vXX.X/query/?q=soql)
  • The QueryAll resource (vXX.X/queryAll/?q=soql)
  • The sObject Collections resource (vXX.X/composite/sobjects). Available in API version 43.0 and later.

 

Composite Rest supports all or none features where we can specify what should happen when an error occurs in subrequest whether it will rollback the whole process or only itself. 

 

 

Simple Composite request:

 

{

“compositeRequest” : [{

  “method” : “POST”,

  “url” : “/services/data/v53.0/sobjects/Account”,

  “referenceId” : “refAccount”,

  “body” : { “Name” : “Sample Account” }

  },{

  “method” : “POST”,

  “url” : “/services/data/v53.0/sobjects/Contact”,

  “referenceId” : “refContact”,

  “body” : { 

    “LastName” : “Sample Contact”,

    “AccountId” : “@{refAccount.id}”

    }

  }]

}

 

This is a simple example of the Composite Rest API capabilities. In this example we are requesting to create an account record and its child contact record in a single call. 

 

Example

 

URL Endpoint: : https://akhillightning-dev-ed.my.salesforce.com/services/data/v53.0/composite

 

Request Type: POST

 

Header: 

  • Authorization: Bearer token
  • Content-Type: application/json

 

Response:

 

{

    “compositeResponse”: [

        {

            “body”: {

                “id”: “0010I00002ZExAbQAL”,

                “success”: true,

                “errors”: []

            },

            “httpHeaders”: {

                “Location”: “/services/data/v53.0/sobjects/Account/0010I00002ZExAbQAL”

            },

            “httpStatusCode”: 201,

            “referenceId”: “refAccount”

        },

        {

            “body”: {

                “id”: “0030I00002QHLCyQAP”,

                “success”: true,

                “errors”: []

            },

            “httpHeaders”: {

                “Location”: “/services/data/v53.0/sobjects/Contact/0030I00002QHLCyQAP”

            },

            “httpStatusCode”: 201,

            “referenceId”: “refContact”

        }

    ]

}

 

 

Composite request body

Here are the composite request body parameter examples:

 

{

   “allOrNone” : true,

   “collateSubrequests”: true,

   “compositeRequest” : [{

      Composite Subrequest

      },{

      Composite Subrequest

      },{

      Composite Subrequest

   }]

}

 

 

    • allOrNone – we can pass true or false. By this we can control the allOrNone feature.
  • collateSubrequests – Controls whether the API collates unrelated subrequests to bulkify them (true) or not (false).
  • compositeRequest – Collection of subrequests to execute.

 

 

Why Composite Resources

  • Multiple REST API calls for a single call.
  • Can complete a process in a single call like we need to complete a series of co-related operations in a single call.
  • Can easily handle Parent-child relationship operation.
  • Good practices to reduce  API usages.
  • Can handle more complicated and related objects and data structures.
  • Useful for allOrNone process situations. Like while using only Rest API if we are inserting an account and its child contact records but while contact creation failed due to some error then we can’t rollback the account record. It can easily be managed in composite rest API.

 

 

Types of Composite Resources

Salesforce has divided Composite resources into the following types.

  • Composite API
  • Batch
  • SObject Tree
  • SObject Collection
  • Graphs

Composite API 

A basic version of Composite resource. It can execute a series of API calls in a single call. It will respond in a single output result.

The following URL can be used to make Composite API calls:

URL : /services/data/vXX.X/composite

Composite Graphs

In composite graphs, we can handle more complex composite requests. The series of request call called node in the Composite Graphs. Composite graphs increase this limit to 500. This gives a single API call much greater power.

Composite Graphs Only supports the following URls.

/services/data/vXX.X/sobjects/sObject POST
/services/data/vXX.X/sobjects/sObject/id DELETE, GET, PATCH
/services/data/vXX.X/sobjects/sObject/customFieldName/externalId DELETE, GET, PATCH, POST

Composite Batch

Executes up to 25 subrequests in a single request. The response bodies and HTTP statuses of the subrequests in the batch are returned in a single response body. Each subrequest counts against rate limits. Subrequests are independent, and you can’t pass information between them. Subrequests execute serially in their order in the request body. When a subrequest executes successfully, it commits its data. Commits are reflected in the output of later subrequests. If a subrequest fails, commits made by previous subrequests are not rolled back. If a batch request doesn’t complete within 10 minutes, the batch times out and the remaining subrequests aren’t executed.

 

URI

/services/data/vXX.X/composite/batch

Composite sObject Tree

sObject Tree is useful for creating parent-child relationships records. We can create sObject Tree up to 5 levels deep. The sObject tree is limited to Up to a total of 200 records across all trees. 

URI: /services/data/vXX.X/composite/tree/sObjectName

Composite SObject Collection

  • Executes actions on multiple records in one request. 
  • One request can handle only one object.
  • Upsert up to 200 records.
  • The list can contain objects of different types, including custom objects.
  • Objects are created in the order they’re listed. 
  • Sample URI POST /services/data/vXX.X/composite/sobjects

Summary: 

A composite resource is an enhanced version of the Rest API. It can complete a series of operations in a single call and is useful when we need to reduce API calls. It’s a great way to complete a whole process (Set of operations) in a single call which benefited resource utilizations. 

Akhil Mandia

Akhil Mandia

Salesforce Developer

Akhil, one of our sophisticated developers, holds a deep passion for coding and software development. He has a background in website development and has continued to grow his passion in the Salesforce Ohana as a Salesforce Developer.

Tweet
Share