简体   繁体   中英

Exposing the Salesforce Data as an Rest API

I am new to Salesforce and I would like to get some expert advice on how I can expose the Sales force data as an Rest API so the external System can consume it. I was think if I can create a Apex Class like below

@RestResource(urlMapping='/GetAccounts/*')
global with sharing class GetAccounts {
    @HttpGet
    global static Account doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account result = [SELECT Id, Name FROM Account WHERE Id = :accountId];
        return result;
    }
}

And for the external user to consume the data I thought I can set up up a Connected App and provide them with the Username,password, Consumer Key,Consumer secret and they should be authenticating in to Salesforce to get the URI and session ID. Using the SessionID and URI the should be able to call the API that is exposed above. Is this the right approach, please let me know if I am missing anything here.

Also there is a requirement to use Swagger with the API, is it possible to use the Swagger within the Apex Class. Can you please help how I can leverage Swagger with my API here.

First of all you should try to use Salesforce standard REST API. You can check the full documentation from here. https://developer.salesforce.com/docs/api-explorer/sobject/Account

You might be asking yourself, well when I should expose an APEX class as a REST API like the code you have provided? You need to do that when you need custom logic to be performed and combined with the API call.

Exposing Salesforce REST API as OPEN API specification(Swagger) is not yet supported. You can vote for this idea if you need it. https://success.salesforce.com/ideaView?id=0873A000000cQsxQAE

But the other way is supported. You can import a swagger specification file and invoke it using point and clicks from Salesforce. Check this blog for more details: https://andyinthecloud.com/2017/07/23/simplified-api-integrations-with-external-services/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM