---
schema_version: "1"
id: 4173c320c6769babd48694e1c3ae309deada20425bf1c2fdf4c9f27b0680dd69
title: Log In to Salesforce with Code
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/xcloud/264-0-0/domain_name_login_code.md
canonical_url: https://help.salesforce.com/s/articleView?id=xcloud.domain_name_login_code.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 4867afacd2b2a99be20e55b7fad2816fe6d9238b134a799f71d58225d5c46657
description: 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.
products:
  - Salesforce Platform
  - Cross Cloud
release: "264"
---
# 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

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: both Salesforce Classic and Lightning Experience</td></tr><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: <strong>Group</strong>, <strong>Essentials</strong>, <strong>Professional</strong>, <strong>Enterprise</strong>, <strong>Performance</strong>, <strong>Unlimited</strong>, and<strong> Developer</strong> Editions</td></tr></tbody></table>

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.com` for production orgs and `https://MyDomainName--SandboxName.sandbox.my.salesforce.com` for sandboxes.
- The default Salesforce login URLs: `https://login.salesforce.com` for production and Developer Edition orgs and `https://test.salesforce.com` for 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.

![Tip](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-xcloud-setup-264-0-0-production-enus/6f21df35-9ea0-4f53-ac94-45c3b5dfd5bc/images/icon_tip.png)

Tip To search your Salesforce code, download the metadata. Then use a code editor such as Microsoft Visual Studio.

## 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](https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_login_loginresult.htm). 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](https://help.salesforce.com/s/articleView?id=xcloud.domain_name_redirections_understand.htm&release=264.0.0&type=5 "After you deploy a change to your My Domain, Salesforce redirects your previous My Domain host names. If your org was created before Summer ’22, enhanced domains were deployed in your org and some of those redirections were temporary. And if your production org or demo org was created before Summer ’20, host names that contained your Salesforce instance could be redirected. Learn more about these redirections and how to control them.").

If you find instanced URLs in your code, replace them with your My Domain login URL or the default Salesforce login URL.

#### See Also

- [My Domain](https://help.salesforce.com/s/articleView?id=xcloud.domain_name_overview.htm&release=264.0.0&type=5 "Showcase your company’s brand with a customer-specific subdomain name in your Salesforce org URLs. With My Domain, you can include your company name in your URLs, for example, https://mycompany.my.salesforce.com. With these org-specific URLs, you can set up a custom login page, set a custom login policy, offer single sign-on, and allow users to log in with a social account. My Domain also allows you to work in multiple Salesforce orgs in the same browser at the same time.")
- [_Apex Reference Guide_: DomainCreator Class](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_DomainCreator.htm)