I’m excited to introduce you to the Salesforce Composite API, a powerful tool designed to streamline our API interactions. By allowing us to perform multiple API requests in a single call, the Composite API not only accelerates our workflows but also ensures data consistency and smooth transactions. Whether you're creating, updating, or deleting related records, the Composite API has got you covered. Let's dive into some detailed examples to see how it works in practice.
Imagine you need to create an account and simultaneously add a related contact and case. Normally, this would require separate API calls for each record, but with the Composite API, you can handle it all in one request.
{
"compositeRequest": [
{
"method": "POST",
"url": "/services/data/v62.0/sobjects/Account",
"referenceId": "NewAccount",
"body": {
"Name": "Nested Account"
}
},
{
"method": "POST",
"url": "/services/data/v62.0/sobjects/Contact",
"referenceId": "NewContact",
"body": {
"FirstName": "prav",
"LastName": "een",
"AccountId": "@{NewAccount.id}"
}
},
{
"method": "POST",
"url": "/services/data/v62.0/sobjects/Case",
"referenceId": "NewCase",
"body": {
"Subject": "New Case for Nested Account",
"AccountId": "@{NewAccount.id}",
"ContactId": "@{NewContact.id}"
}
}
]
}
Next, let’s say you need to delete multiple records like an account, a contact, and an opportunity. The allOrNone parameter ensures that either all deletions happen, or none do, maintaining data integrity.
{
"allOrNone": true,
"compositeRequest": [
{
"method": "DELETE",
"url": "/services/data/v62.0/sobjects/Account/001dM00002JS9fZQAT",
"referenceId": "DeleteAccount"
},
{
"method": "DELETE",
"url": "/services/data/v62.0/sobjects/Contact/003XXXXXXXXXXXXXXX",
"referenceId": "DeleteContact"
},
{
"method": "DELETE",
"url": "/services/data/v62.0/sobjects/Opportunity/006XXXXXXXXXXXXXXX",
"referenceId": "DeleteOpportunity"
}
]
}
Now, let’s look at how to update multiple records in one call. We’ll update the phone number and website of an account, and the phone number and email of a contact.
{
"compositeRequest": [
{
"method": "PATCH",
"url": "/services/data/v62.0/sobjects/Account/{AccountId}",
"referenceId": "UpdateAccount",
"body": {
"Phone": "987-654-3210",
"Website": "https://www.updatedweb.com"
}
},
{
"method": "PATCH",
"url": "/services/data/v62.0/sobjects/Contact/{ContactId}",
"referenceId": "UpdateContact",
"body": {
"Phone": "987-654-3210",
"Email": "updatedemail@twst.com"
}
}
]
}
Lastly, let’s update records using specific IDs. This example is similar to the previous one but uses actual record IDs.
{
"compositeRequest": [
{
"method": "PATCH",
"url": "/services/data/v62.0/sobjects/Account/001XXXXXXXXXXXXXXX",
"referenceId": "UpdateAccount",
"body": {
"Phone": "987-654-3210",
"Website": "https://www.updated-website.com"
}
},
{
"method": "PATCH",
"url": "/services/data/v62.0/sobjects/Contact/003XXXXXXXXXXXXXXX",
"referenceId": "UpdateContact",
"body": {
"Phone": "987-654-3210",
"Email": "updated-email@demo.com"
}
}
]
}
In this example, we will retrieve an account and its related contacts and opportunities.
{
"compositeRequest": [
{
"method": "GET",
"url": "/services/data/v62.0/sobjects/Account/001dM00002JS9fZQAT",
"referenceId": "AccountInfo"
},
{
"method": "GET",
"url": "/services/data/v62.0/query/?q=SELECT+Id,FirstName,LastName,Email,Phone+FROM+Contact+WHERE+AccountId='@{AccountInfo.id}'",
"referenceId": "AccountContacts"
},
{
"method": "GET",
"url": "/services/data/v62.0/query/?q=SELECT+Id,Name,StageName,CloseDate,Amount+FROM+Opportunity+WHERE+AccountId='@{AccountInfo.id}'",
"referenceId": "AccountOpportunities"
}
]
}
In conclusion, the Salesforce Composite API is a versatile tool that allows you to perform multiple operations in a single API call. Whether you are creating nested records, deleting multiple records with the all-or-none flag, or updating multiple records, the Composite API makes the process efficient and consistent.
I hope these detailed examples have given you a clear understanding of the Composite API's capabilities and how you can use it to simplify your work in Salesforce.

We are a team dedicated to continuous learning. Every Week, we explore new ideas, gain fresh insights, and turn our discoveries into valuable blog posts. Follow along as we grow, share, and evolve—one lesson at a time!