REST API provide a powerful and simple Web Services API for interacting with Force.com. Its advantages is easy to integrate and development. However, REST API is the best option, if you have many records to process. REST API uses the same underlying data model and standard objects as like in SOAP API.
REST API is the best example of abstraction, such as single record, collection of records or even dynamic real-time data. REST API is identified by named URL and is accessed by using standard HTTP methods like (HEAD, GET, POST, PATCH, DELETE). REST API can be use to interact with Force.com object. For example:
- Obtain detailed information about the Standard/Custom object.
- Perform Query/Search options.
- Add/Update or Delete records.
Force.com API supports OAuth an open source protocol to allow secure API authentication. It also supports for JSON and XML format. JSON is the default format for REST API, you can change this to XML. You can use many tools like PostMan or CURL to send HTTP request to access REST API.
- SalesForce.com supports OAuth protocol to allow users of application to securly access the data without having real username and password. Before making any API request, you must authenticate the application using OAuth.
- Set up your application as a connected app in the Salesforce organization.
- Determine the correct Salesforce OAuth endpoint for your connected app to use. Supported OAuth flows include:
- Web server flow, where the server can securely protect the consumer secret.
- User-agent flow, used by applications that cannot securely store the consumer secret.
- Username-password flow, where the application has direct access to user credentials.
- After successfully authenticating the connected app user with Salesforce, you’ll receive an access token which can be used to make authenticated REST API calls.
Creating Connected APP:
- From Setup, enter Apps in the Quick Find box, then select Apps and click New to start defining a connected app.
- Enter the name of your application.
- Enter the contact email information, as well as any other information appropriate for your application.
- Select Enable OAuth Settings.
- Enter a Callback URL. Depending on which OAuth flow you use, this is typically the URL that a user’s browser is redirected to after successful authentication. As this URL is used for some OAuth flows to pass an access token, the URL must use secure HTTP (HTTPS) or a custom URI scheme.
- Add all supported OAuth scopes to Selected OAuth Scopes. These scopes refer to permissions given by the user running the connected app.
- Enter a URL for Info URL. This is where the user can go for more information about your application.
- Click Save. The Consumer Key is created and displayed, and the Consumer Secret is created (click the link to reveal it).
curl -X POST -H “Content-Type: application/x-www-form-urlencoded” -H “Cache-Control: no-cache” -H “Postman-Token: 26a0df06-9cc6-d1a5-2945-bce6a12a40b4” -d ‘client_id=3MVG9ZL0ppGP5UrBctV8ANK6.aSwde.V2awsdMdFLRjJwx_vAoetz0GFD2iM2zoLqwefGtgFy.1b1Sx23d44fpemail@example.com&password=test@123AsqswDeXGRWCR4zMGU6xnFMz’ “https://login.salesforce.com/services/oauth2/token”
Introducing Chatter REST API:
Use Chatter REST API to display Salesforce data, especially in mobile applications. Chatter provide feeds, users, groups, and followers, Chatter REST API provides programmatic access to files, recommendations, topics, notifications, Data.com purchasing, and more. Chatter REST API is similar to APIs offered by other companies with feeds, such as Facebook and Twitter, but it also exposes Salesforce features beyond Chatter.
When to Use Chatter REST API:
- Build a mobile app.
- Integrate a third-party Web application with Salesforce.
- Display a feed on an external system.
Chatter REST API Architecture:
- To use Chatter REST API, understand a few characteristics of its architecture, like Chatter REST API uses OAuth 2.0.
- Chatter REST API responses are minimally HTML entity-encoded by default.
- For non-HTML contexts, such as native mobile applications, Chatter REST API clients can request raw (unencoded) output.
- The JSON format is supported with UTF-8. Date-time information is in ISO8601 format.
- Responses are in JSON format by default. To specify JSON, use Accept: application/json.)
- Salesforce IDs in response bodies are always 18 character IDs. In request bodies, you can use either 15 character IDs or 18 character IDs.
- Chatter REST API requests are subject to rate limiting. Chatter REST API has a different rate limit than other Salesforce APIs. Chatter REST API has a per user, per application, per hour rate limit. When you exceed the rate limit, all Chatter REST API resources return a 503 Service Unavailable error code.
- For applications using a session ID from Force.com, the rate limit is per user, per hour. To take advantage of the per user, per application, per hour limit, use OAuth tokens.
Set Up Authorization: Create a connected app in a Salesforce organization and enable OAuth. The client application uses the connected app to connect to Salesforce. Let’s create a connected application on the first step to make an authentication for requester.
- Enter a Callback URL. It must be secure: http:// does not work, only https:// works.
- Go to Setup and create a new connected application, as shown in picture.
- Enter a connected app name.
- Enter the contact email, as well as any other required information.
- Select Enable OAuth Settings.
- Enter an OAuth scope. Select Access and manage your Chatter feed in addition to any other scope you want your connected app to allow access to and click Save.
- The Consumer Key is created and displayed, and a Consumer Secret is created (click the link to reveal it).
Connect to Chatter REST API Using OAuth: Use OAuth to connect to Salesforce and get an access token. Pass the access token in requests to Chatter REST API. There are multiple way you can access API but easier way is using curl. Curl Command to generate access token through connected app using command line tool. To make a request to Salesforce:
Step 2: To request a user information, use the returned “id”. Pass the returned access_token as a Bearer token in the Authorization request header and get the details.
Step3: Post a New Comment On Chatter
||Post feed elements and search all feed elements in an organization.
||Returns all feed elements posted with files by people or groups that the context user follows.
||All feed elements from all groups the context user either owns or is a member of, as well as all files, records, and users the context user follows. Use this resource to get information about feed elements and to post feed elements.
||Returns the feed elements for all the records the context user is following, or all the feed elements of the specified recordId. Use this resource to search a feed or to get the feed elements for a specific feed, including another user’s feed. To use this resource to get the feed elements for a group, specify the group ID as the recordId.
||Returns feed elements created when a user changes records that can be tracked in a feed, feed elements whose parent is the user, and feed elements that mention the user. This feed is different than the news feed, which returns more feed items, including group updates. You can post feed items to the user-profile feed. You can get another user’s user profile feed.
||GET, POST, PATCH, DELETE
||Get information about a specified file, including references to external files. Upload a new version of an existing file, including references to external files. Rename a file, move a file to a different folder, and delete a file.
||Returns the content of the file, including references to external files. The content is streamed as the body of the response.
||Returns information about the objects with which the specified file has been shared. Objects can be users, groups, or records. Can also be used to share the specified file with users.
||GET, PUT, DELETE
||A description of a file shared as a link. Create, access, and delete a file’s share link.
||Get information about a file’s supported previews and generate a file preview.
||Get a file preview or a specific page in a preview.
||A rendition of the file. A rendition is a binary preview of the file. The format of the binary depends on the format specified in the request.
||Information about a list of files, specified by file IDs. Get information about a list of files and delete a list of files.
||GET, PATCH, DELETE
||Get information about the specified folder. Move, rename, and delete a folder. Sync the root folder.
||Get information about the contents of a folder. Add a file to a folder and create a folder. Sync the root folder.
||Returns information about the files posted to the specified group.
||Returns information about the five files most recently posted to the specified topic.
||Upload a file, including references to external files, to the Files home or get information about files a user owns. These files don’t include files shared with a user, files a user follows, or general organization files.
||Returns information about files posted to groups that the specified user is a member of.
||Returns information about files that have been shared with the specified user.