0

I started reading on REST API’s and have created this class to view order tracking information:

@RestResource(urlMapping=‘/Tracking/*')
global without sharing class TrackingRest_Service {

    @HttpGet 
    global static Order getOrderTracking(){
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String tracking = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Order ord = [SELECT Tracking_Number__c From Order WHERE Tracking_Number__c =: tracking];
        return order;
    }
}

However I’m not sure how I can expose it. Basically we want to just be able to call this endpoint and allow our external service developers to display the tracking information.

1

I tend to use one of two options:

  1. If it's public data that I'm serving, then I create a Site and give the Site Guest User access to my Rest class. Then, anyone can access your service. For a Site called public, the URL ends up being https://myCompany.force.com/public/services/apexrest/Tracking
  2. If it's something that should only be accessible by a logged-in user, then it is simply accessed by a URL like this https://na24.salesforce.com/services/apexrest/Tracking where the Authorization header is a Bearer Token obtained via a SOAP login (see https://developer.salesforce.com/docs/atlas.en-us.api_asynch.meta/api_asynch/asynch_api_quickstart_login.htm)
0

Id suggest you go through Exposing Apex Classes as REST Web Services

you have the first part (which is the annotadet Apex Rest MEthod)

Now you need to expose it, typically, this is done via a Site

Once you have created the site, if you need to expose it (unauthenticated) make sure that the new site has guest user access and the the profile has access to the apex class.

When you select an Apex class, it has a security setting to enable profile Access, you will have to add the guest site profile. enter image description here

For authenticated Rest Call, I usually setup a Conencted app + associate to a user + profile, still need to enable the profile for the apex class and expose via Sites,

Quick Tip – Public RESTful Web Services on Force.com Sites

0

However I’m not sure how I can expose it. Basically we want to just be able to call this endpoint and allow our external service developers to display the tracking information.

The time you have written a web service class, it's now exposed. It's just how you need to call it.

But before any external client can invoke your REST webservice, they need to authenticate themselves. And the way you authenticate to be able to consume any REST API, is using OAuth 2.0 where a token can be retrieved by setting up a Connected App. Refer to the documentation for more details on authentication.

Before making REST API calls, you must authenticate the application user using OAuth 2.0

Once the client has authenticated themselves, they can invoke the GET method as in your API by using the below endpoint URL and passing the OAuth token.

https://your-instance-url/services/apexrest/<namespace if any>/Tracking

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.