Log In to Salesforce with Code
For an extra layer of security, use your My Domain login URL to access your Salesforce org with code. Compare the benefits of your My Domain login URL versus the default Salesforce login URL. And understand why we recommend that you update the login URLs that contain your Salesforce instance.
Required Editions
| Available in: both Salesforce Classic and Lightning Experience |
| Available in: Group, Essentials, Professional, Enterprise, Performance, Unlimited, and Developer Editions |
There are two options to log in to Salesforce with code, such as API calls and Apex code.
- Your My Domain login URL, in the format
https://MyDomainName.my.salesforce.comfor production orgs andhttps://MyDomainName--SandboxName.sandbox.my.salesforce.comfor sandboxes. - The default Salesforce login URLs:
https://login.salesforce.comfor production and Developer Edition orgs andhttps://test.salesforce.comfor sandboxes.
You can use more than one option. For example, you can use your My Domain login URL for all new code while still using default login URLs in your existing code.
Recommended: Your My Domain Login URL
Your My Domain login URL contains your company-specific My Domain name. Because My Domain names are unique, your My Domain login URL adds another layer of security.
Admins can require SOAP API calls to log in with the org’s My Domain login URL via a My Domain setting. Also, this login URL continues to work when your org moves to another instance.
For these reasons, we recommend that you use your My Domain login URL to access your org with code.
There’s one consideration when you use your My Domain login URL with code. If you rename your My Domain, for example, when your brand or company name changes, your My Domain login URL changes. In this situation, your code breaks until you update the references to the previous My Domain login URL in your code.
For API integrations, use the metadataServerUrl or
serverURL value returned by a login request. To get the host name of your
My Domain login URL in Apex, use the getOrgMyDomainHostname() method of the System.DomainCreator class. These methods continue to work after a My Domain
change.
Default Salesforce Login URLs
The default login URLs of https://login.salesforce.com for production and
Developer Edition orgs and https://test.salesforce.com for sandboxes work
well in code. These URLs continue to work when your org moves to another instance and when
you rename your My Domain.
These default login URLs don’t have the extra layer of security of including your company-specific My Domain name. Also, an admin can require SOAP API calls to log in with the org’s My Domain login URL via a My Domain setting. When that setting is enabled, these default login URLs don’t work in SOAP API calls.
Replace Instanced Salesforce URLs
We don’t recommend instanced URLs when logging in to Salesforce with code or as a user. For example, swe54.sfdc-cehfhs.salesforce.com or na128.salesforce.com. First, API calls to a Hyperforce org can’t use that method. Second, when your org is moved to another Salesforce instance, code that uses the instanced URL breaks. Most importantly, support for incorrect instanced URLs in API calls ends on a rolling basis shortly after your org gets the Summer ’26 release. For more information, see Understand Redirections for Previous My Domain Host Names.
If you find instanced URLs in your code, replace them with your My Domain login URL or the default Salesforce login URL.

