To access chatter rest api using APEX code, I need salesforce instance server url in following format:


Is there any method which works in all cases

Case 1: Any Development Org

Case 2: My Domain Enabled

Case 3: Sandbox

Case 4: Managed Package


6 Answers 6


You are looking for URL.getSalesforceBaseUrl().


The Winter 19 release is introducing another method to get the URL, which will give you the canonical URL for your org: URL.getOrgDomainUrl().

Returns the canonical URL for your org. For example, https://yourDomain.my.salesforce.com or, for orgs without My Domain enabled, https://yourInstance.salesforce.com.

Both of the above methods return a System.URL. If you would like the string version, simply call .toExternalForm() on that instance. For example:

String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
  • 9
    System.Url.getSalesforceBaseURL().toExternalForm() to be more precise. Jan 29, 2015 at 3:05
  • 6
    Any idea how to get your instance if you have mydomain enabled ? Jul 1, 2016 at 12:41
  • 1
    @SamuelDeRycke You need to use the status/trust API. See api.status.salesforce.com/v1/docs/#!/instances/… Feb 6, 2017 at 19:50
  • @SamuelDeRycke Didn't see that you had pinged this answer until just now.
    – Adrian Larson
    Feb 6, 2017 at 19:51
  • 2
    @SamuelDeRycke Salesforce is now adding new method in Winter19 i.e. System.Url.getOrgDomainUrl() Aug 25, 2018 at 4:50

To retrieve a URL object containing the full beginning portion of the URL, such as "https://na17.salesforce.com", you may use the static method:

    System.URL.getSalesforceBaseURL();  // Yields URL object for https://xxxx.salesforce.com

As Ashish notes above, suffixing this method with ".toExternalForm()" will convert the URL object into its text representation, as follows:

    System.URL.getSalesforceBaseURL().toExternalForm();  // Yields string "https://xxxx.salesforce.com"

Furthermore, if you wish to retrieve only the subdomain portion of the URL (the "na17" part to the left of the ".salesforce.com" domain), use:

    System.URL.getSalesforceBaseURL().getHost();  // Yields subdomain (host) as string

These methods will work reliably across Production, Sandboxes, Dev Orgs, etc. Which one you choose depends upon your specific need in the code that you are writing.

Lastly, if you're trying to create a host-independent object link, you may do so as follows:

    String link = '<a href="' + System.URL.getSalesforceBaseUrl().toExternalForm() + '/' + sObject.Id + '" target="_blank">sObject Link</a>';
  • 4
    Any idea how to get your instance if you have mydomain enabled ? Jul 1, 2016 at 12:42

I used this approach to get just the instance, as in managed packages the other one approaching is not working for me

String urlInstance = String.valueof(System.URL.getSalesforceBaseURL()).replace('Url:[delegate=','').replace(']','');
String[] instance = urlInstance.split('\\.');

Hope it helps.


You can get the instance name from the Organization object using:

select InstanceName from Organization

Unlike the solutions using 'System.URL.getSalesforceBaseURL()...' this will work for you in:

Case 1: Any Development Org Case 2: My Domain Enabled Case 3: Sandbox Case 4: Managed Package

  • By virtue of its simplicity and elegance, yours is the most useful solution, and maybe the only one that can be accomplished quickly in a developer console. You need only enter the one-line SOQL query into the built-in Query Editor. Nov 21, 2020 at 9:31

The OLD way was to use getSalesforceBaseUrl()

That has been deprecated. From the docs:

In API version 59.0 and later, this method is deprecated and versioned out. Use getOrgDomainUrl() to get the canonical URL for your org or use getCurrentRequestUrl() to get the URL of an entire request on a Salesforce instance. Returns the URL of the current connection to the Salesforce org.


Returns the canonical URL for your org. For example, https://MyDomainName.my.salesforce.com. (Use this for the forseeable future.)



DomainCreator class is now provided by Salesforce, we can use this class to get different urls.

Salesforce documentation link for reference:


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .