Sub topic 6
Project-level settings: sourceApiVersion, sfdcLoginUrl, namespace, name
This chapter covers the four top-level keys in sfdx-project.json that apply to the whole project rather than to one package directory. It assumes you have generated a project with sf project generate, you can read JSON, and you have authorized at least one org with the Salesforce CLI (sf).
Where these settings sit
Open the file from a generated project. Everything outside packageDirectories is project-level:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}The CLI reads this file every time you run a project command. It starts in the current directory and walks up the folder tree until it finds sfdx-project.json. The values below therefore apply to every command you run anywhere inside the project.
| Key | Type | Purpose | Typical value |
|---|---|---|---|
| `sourceApiVersion` | string | API version used for source operations (deploy, retrieve, convert) | `"61.0"` |
| `sfdcLoginUrl` | string | Login URL the CLI uses when you authorize an org | `"https://login.salesforce.com"` |
| `namespace` | string | Namespace of the packages or orgs this project works with | `""` or `"acme"` |
| `name` | string | Human-readable project name | `"my-project"` |
sourceApiVersion
Every Salesforce release adds a numbered API version, and metadata types and fields differ between versions. sourceApiVersion pins the version the CLI uses when it moves your source to and from an org.
What it controls
- Deploys. The CLI builds a manifest (
package.xml) for the deploy. That manifest carries the version fromsourceApiVersion. - Retrieves. The CLI asks the org for metadata at that version. A type or field introduced in a later release will not come back.
- Conversions. Commands that convert between source format and metadata format write this version into the output.
Individual components also carry their own version. An Apex class has an apiVersion element in its -meta.xml file. That value controls how the class behaves at run time. sourceApiVersion only controls the tooling operation. The two can differ without causing an error.
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<status>Active</status>
</ApexClass>Here the class runs at 58.0 while a deploy of the project may use 61.0.
Overriding it
You can override the project value for your machine without editing the file. This is useful when you work in an org that is on an older release, such as a sandbox preview window.
sf config set org-api-version 60.0
sf config get org-api-versionThe org-api-version setting takes precedence over sourceApiVersion for commands that honor it. Use sf config unset org-api-version to go back to the project value.
Choosing a value
- Set it to a version your target org supports. An org cannot accept a version newer than its own release.
- Keep it the same for everyone on the team, and commit the file. A mismatch produces noisy diffs when two developers retrieve the same component.
- If you leave the key out, the CLI falls back to a default it picks itself. That default changes when the org or the CLI is upgraded, so results can change without any edit in your repository. Pin the value.
sfdcLoginUrl
sfdcLoginUrl is the default login endpoint for sf org login web. Its default, https://login.salesforce.com, is correct for production orgs and Developer Edition orgs. Sandboxes sign in at a different host, so a project that targets a sandbox needs a different value.
| Org type | Login URL |
|---|---|
| Production, Developer Edition | `https://login.salesforce.com` |
| Sandbox | `https://test.salesforce.com` |
| Org using My Domain | `https://mycompany.my.salesforce.com` |
| Sandbox with My Domain | `https://mycompany--qa.sandbox.my.salesforce.com` |
Set the project default in the file, or override it for a single login with a flag:
sf org login web --alias qa-sandbox --instance-url https://test.salesforce.comThe --instance-url flag wins over the file. The diagram shows how the CLI picks the URL.
Common mistakes
- Logging in to a sandbox without changing the URL. The browser shows a failed login even though your credentials are right.
- Hard-coding a sandbox URL in a project that deploys to production. The next developer who logs in to production fails the same way. For projects that target several org types, leave the file at the default and pass
--instance-urlper login. - Adding a trailing path such as
/login. Use the host only.
sfdcLoginUrl only affects authorization. Once an org is authorized, the CLI stores that org's own instance URL, and deploys and retrieves use the stored value.
namespace
A namespace is a short, globally unique prefix that Salesforce attaches to the components of a managed package or a namespaced unlocked package. It is registered in a special org, then linked to your Dev Hub.
In sfdx-project.json, namespace declares which namespace this project's packages use:
{
"namespace": "acme"
}When the project has no namespace, use an empty string. That is what sf project generate writes.
What it affects
- Package creation.
sf package createfor a package in this project uses the namespace. Once a package version has been created with a namespace, you cannot remove or change it for that package. - Scratch orgs. A scratch org created from a project with a namespace gets that namespace. Component names in that org are prefixed, for example
acme__Invoice__c. - Source files. The CLI does not add the prefix to your source files. You write
Invoice__cin the repository, and the namespace is applied in the org.
To create a scratch org without the namespace, which is common when you want to test unprefixed code, pass --no-namespace:
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias plain-scratch \
--no-namespacename
name is a label for the project. The CLI does not use it to find, build, or deploy anything. Tools such as editors may show it, and it helps people tell projects apart. Keep it short, and match the repository name so that it stays accurate.
name is not the same as a package name. A package's name is set in the package field of a package directory, which a later chapter covers.
Checking the settings from a script
A short script can confirm that all four keys are present before a CI job runs. This one uses only the Python standard library and exits with an error if something is wrong.
import json
import sys
from pathlib import Path
REQUIRED = ["name", "namespace", "sfdcLoginUrl", "sourceApiVersion"]
def main(path: str = "sfdx-project.json") -> int:
try:
project = json.loads(Path(path).read_text(encoding="utf-8"))
except FileNotFoundError:
print(f"{path} not found")
return 1
except json.JSONDecodeError as err:
print(f"Invalid JSON in {path}: {err}")
return 1
missing = [key for key in REQUIRED if key not in project]
if missing:
print("Missing keys: " + ", ".join(missing))
return 1
version = project["sourceApiVersion"]
if not isinstance(version, str) or not version.replace(".", "", 1).isdigit():
print(f"sourceApiVersion should look like '61.0', got {version!r}")
return 1
print(f"{project['name']}: API {version}, login {project['sfdcLoginUrl']}")
return 0
if __name__ == "__main__":
sys.exit(main(*sys.argv[1:]))Note that the version is checked as a string. Writing 61.0 without quotes is valid JSON, but it becomes a number and trailing zeros can be lost in some tools. Always quote it.
Troubleshooting these settings
| Symptom | Likely cause | Fix |
|---|---|---|
| Sandbox login page rejects valid credentials | Login URL points to production | Pass `--instance-url https://test.salesforce.com` or set `sfdcLoginUrl` |
| Deploy fails with an error about an unsupported API version | `sourceApiVersion` is newer than the org's release | Lower the value, or set `org-api-version` for that org |
| A new metadata field is missing after retrieve | `sourceApiVersion` is older than the release that added the field | Raise the value |
| Component names have an unexpected prefix in a scratch org | Project `namespace` is set | Use `--no-namespace` or clear the key |
| CLI reports a parse error on the file | A trailing comma or missing quote in the JSON | Validate the file, for example with the script above |
Exercise
A teammate's sf org login web opens the sandbox login page and rejects the correct password for a sandbox user. The project's sfdcLoginUrl is https://login.salesforce.com. Give two ways to fix the login without changing how production logins behave for the rest of the team.
Show answer
Pass the sandbox URL for that login only: sf org login web --alias qa-sandbox --instance-url https://test.salesforce.com. The flag overrides sfdcLoginUrl. Alternatively, use the sandbox's My Domain URL in the same flag, for example https://mycompany--qa.sandbox.my.salesforce.com. Both leave the committed sfdcLoginUrl unchanged.
Exercise
Your project sets sourceApiVersion to 61.0, but an Apex class's -meta.xml says apiVersion 58.0. Which version does the deploy manifest use, and which does the class run at?
Show answer
The deploy manifest uses 61.0 from sourceApiVersion. The class runs at 58.0, the value in its own -meta.xml file. The two settings control different things.
Exercise
Why should you set sourceApiVersion explicitly instead of leaving it out?
Show answer
When it is missing, the CLI chooses a default that can change with the org or CLI version. Pinning it makes deploys and retrieves repeatable and keeps diffs consistent across the team.