Sub topic 11
UI API and Connect REST API for user-interface use cases
This chapter assumes you can read HTTP requests and JSON, know Salesforce objects, fields, and page layouts, and have followed the REST API and authentication chapters. Everything here uses the same OAuth access token and My Domain host as the standard REST API.
Why a UI-focused API exists
The standard REST API returns raw data. A query for an Account gives you stored field values, and nothing else. A custom user interface needs more than that. It must know which fields the user may see, how to label them, how to format a date for the user's locale, which picklist values are valid for a record type, and which buttons to show.
You could assemble all of that from the REST API, but it takes many calls and a lot of code. Two APIs shift that work to Salesforce:
- UI API returns records together with the metadata needed to render them.
- Connect REST API exposes the social and experience features of the platform: Chatter feeds, files, groups, topics, and Experience Cloud sites.
Both are REST-style, return JSON, and live under /services/data/vXX.X/. The Connect REST API is also available in Apex, in the ConnectApi namespace.
UI API: records plus rendering metadata
UI API endpoints sit under /ui-api/. The most useful ones are in this table.
| Resource | Returns |
|---|---|
| `/ui-api/records/{recordId}` | One record, with the fields you request |
| `/ui-api/record-ui/{recordIds}` | Records plus their layout and object metadata in one response |
| `/ui-api/object-info/{objectApiName}` | Field definitions, labels, record types, and permissions for an object |
| `/ui-api/layout/{objectApiName}` | The page layout for an object |
| `/ui-api/list-ui/{objectApiName}/{listViewApiName}` | The records and column definitions of a list view |
| `/ui-api/object-info/{objectApiName}/picklist-values/{recordTypeId}/{fieldApiName}` | Valid picklist values for a record type |
A record request looks like this:
curl -G "https://MyDomain.my.salesforce.com/services/data/v60.0/ui-api/records/$ACCOUNT_ID" \
--data-urlencode "fields=Account.Name,Account.Industry,Account.AnnualRevenue" \
-H "Authorization: Bearer $ACCESS_TOKEN"The response differs from a REST API record in three ways.
- Each field is an object with both
valueanddisplayValue. A currency field might have a raw value of1500000and a display value of$1,500,000.00, formatted for the user's locale and currency settings. - Field names are not prefixed by the object, and the record carries
recordTypeInfo,lastModifiedDate, and similar properties. - Only fields the running user can read are returned. UI API enforces field-level security and sharing, and it respects the user's locale and time zone.
Request only the fields you need
The fields parameter lists fields that must be returned. If you have to ask for a field the user may not be able to read, use optionalFields instead. A request with fields fails if the user lacks access to any listed field. A request with optionalFields silently omits the ones the user cannot see. For a screen shared by users with different profiles, optionalFields is the safer choice.
UI API in Lightning Web Components
Most developers meet UI API through Lightning Web Components (LWC). The wire adapters in lightning/uiRecordApi, such as getRecord, call UI API for you, and they cache results on the client. When one component edits a record, other components showing it are refreshed automatically.
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
export default class AccountCard extends LightningElement {
@api recordId;
@wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD, INDUSTRY_FIELD] })
account;
get name() {
return getFieldValue(this.account.data, NAME_FIELD);
}
get industry() {
return getFieldValue(this.account.data, INDUSTRY_FIELD);
}
}The $recordId string makes the wire reactive. When recordId changes, the adapter fetches the new record. Importing field references from @salesforce/schema lets Salesforce check that the fields exist and track dependencies, so you do not hard-code field names as strings.
If you build with LWC on the platform, use the wire adapters, lightning-record-form, or lightning-record-edit-form. Direct HTTP calls to UI API are for clients outside the platform, such as a mobile app or a custom front end that signs in with OAuth.
Writing records with UI API
UI API also supports creating, updating, and deleting records. The write operations run the same validation as the Salesforce user interface: required fields, validation rules, and record type rules. Updates use PATCH on /ui-api/records/{recordId} with a body containing a fields map. UI API writes are one record at a time, so it is a poor fit for data loading. Use the REST API, Composite, or Bulk API 2.0 for that.
Connect REST API: social and experience features
Connect REST API exposes features that are not plain records. The common resource families are:
- Chatter: feeds, feed elements, comments, likes, and mentions under
/chatter/. - Files: upload, share, and retrieve file versions and sharing links.
- Groups and users: Chatter groups, membership, and user profiles.
- Experience Cloud: sites, navigation menus, and related resources under
/connect/communities/. - CMS and other features: managed content, recommendations, and topics.
To post a message to an account's feed:
curl -X POST "https://MyDomain.my.salesforce.com/services/data/v60.0/chatter/feed-elements" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"feedElementType": "FeedItem",
"subjectId": "001000000000001AAA",
"body": {
"messageSegments": [
{ "type": "Text", "text": "Contract renewed for another year. " },
{ "type": "Mention", "id": "005000000000001AAA" }
]
}
}'Replace the two example Ids with real ones from your org. The body is a list of message segments, not a plain string. Each segment has a type such as Text, Mention, Hashtag, or Link. This structure is what lets Salesforce render a mention as a link to a user and notify that user.
You can write a feed post as a FeedItem record through the REST API, but then you must supply the raw body text and manage mentions yourself. Connect REST API understands mentions, topics, and rich text, so it is the better choice for any feature that behaves like Chatter.
Using ConnectApi in Apex
Apex code can call the same features without HTTP, through the ConnectApi namespace. For example, ConnectApi.ChatterFeeds.postFeedElement posts to a feed from a trigger or a scheduled job. Calls made this way do not use the HTTP API request allocation, because they run inside the platform. They do run under Apex governor limits, and ConnectApi methods have their own per-transaction call limits.
Correcting the above: the diagram block must use the required fence.
How the pieces fit
Edge cases and common errors
- Missing fields in the response. UI API drops fields the user cannot read when you used
optionalFields. Code that assumes a field is always present will break for restricted profiles. Check fornullbefore reading.value. - Request fails with a field error. A field listed in
fieldsthat the user cannot access, or that does not exist, makes the whole request fail. Move non-essential fields tooptionalFields. - Unsupported objects. UI API does not support every object. If a request returns an error saying the object is not supported, fall back to the REST API.
- Treating UI API as a bulk reader. UI API responses are large because they carry metadata. Use SOQL through the REST API for reports and exports.
- Version drift. Resources and response shapes change between API versions. Pin the version in the URL and test before upgrading.
- Counting against limits. Calls to UI API and Connect REST API made over HTTP count toward your org's API request limits, like other REST calls. The limits chapter covers how to monitor them.
When to choose each
| Requirement | Best fit |
|---|---|
| Custom screen that must match page layouts and field permissions | UI API |
| LWC showing or editing a record | LWC wire adapters (UI API under the hood) |
| Post to Chatter with mentions and topics | Connect REST API |
| Upload and share files, manage groups | Connect REST API |
| Read a few fields for a back-end integration | REST API |
| Load 500,000 records | Bulk API 2.0 |
The pattern is the same as in the rest of this tutorial. Pick the API that already does the work your client would otherwise have to rebuild. UI API saves you from rebuilding the Salesforce rendering rules, and Connect REST API saves you from rebuilding the social and site features.
Exercise
A mobile app signs in with OAuth and shows an Account detail screen. Users have different profiles, and some cannot see AnnualRevenue. The screen must show currency in each user's locale. Which API fits, and how should you request AnnualRevenue so the screen does not fail for restricted users?
Show answer
Use UI API, because it returns formatted display values and enforces field-level security. Request AnnualRevenue in the optionalFields parameter, not fields. A request that lists an inaccessible field in fields fails, while optionalFields omits the field for users who cannot read it. The app should then check that the field is present before showing it.
Exercise
You must post an update to a Chatter feed that mentions a user so that the user is notified. Why is Connect REST API preferred over inserting a FeedItem record with the REST API?
Show answer
Connect REST API takes the body as message segments, including a Mention segment, so Salesforce renders the mention as a link and notifies the user. With a plain FeedItem insert, you would have to supply the body text and handle mention markup yourself.