Sub topic 3
Building resource URLs: instance, version, and sObject
This chapter assumes you know basic HTTP (methods, headers, status codes), can read JSON, and have an access token for a Salesforce org from the previous chapter. Every request in the rest of the tutorial uses a URL built from three parts. Getting them right avoids most "404 Not Found" and "wrong org" errors.
The anatomy of a resource URL
A Salesforce REST resource URL has this shape:
https://<instance URL>/services/data/<version>/<resource path>For the Account sObject, a full URL looks like this:
https://MyDomain.my.salesforce.com/services/data/v60.0/sobjects/Account| Part | Example | Meaning |
|---|---|---|
| Instance URL | `https://MyDomain.my.salesforce.com` | The host that serves your org |
| Fixed prefix | `/services/data` | The entry point for the REST API |
| API version | `v60.0` | Which release of the API you call |
| Resource path | `/sobjects/Account` | What you want to work with |
The instance URL
The instance URL is the host name of your org. Do not hard-code it or guess it. Use the instance_url value returned with your access token, because the token is only valid on that host. Sending a valid token to a different instance fails with an authentication error.
Store it once and reuse it:
export SF_INSTANCE_URL="https://MyDomain.my.salesforce.com"
export SF_API_VERSION="v60.0"Do not end the instance URL with a slash. If you do, your joined URL contains //services/data, which is a common cause of confusing failures.
The API version
The version segment, such as v60.0, pins your request to the behavior of one API release. Salesforce publishes new versions regularly and keeps old ones working, so a script that pins v60.0 keeps behaving the same after an upgrade of your org.
Two practical rules follow:
- Put the version in one variable, as above, so you can change it in one place.
- Do not use a version that your org does not support. A version number that is too new returns an error.
You can ask the org which versions it supports. This request needs no resource path beyond the prefix:
curl "$SF_INSTANCE_URL/services/data/" \
-H "Authorization: Bearer $SF_ACCESS_TOKEN"The response is a JSON list of version objects, each with a version and a url.
Resource paths for sObjects
An sObject is a Salesforce object type, such as Account or Contact. Under the sobjects path, the URL shape tells the API what you want:
| URL path | Meaning |
|---|---|
| `/sobjects/Account` | The Account object as a whole. You POST here to create a record. |
| `/sobjects/Account/001xx000003DGb2AAG` | One specific record, identified by its record ID. You GET or PATCH here. |
Object names are case-sensitive in practice for custom objects, which end in __c, for example /sobjects/Invoice__c. Use the API name, not the label shown in the Setup screens.
Query parameters
Anything after ? is a query parameter. For record reads, fields limits what comes back:
/sobjects/Account/001xx000003DGb2AAG?fields=Name,IndustryValues with spaces or quotes need URL encoding. This matters most for SOQL, which a later chapter covers. In curl, -G with --data-urlencode does the encoding for you.
Building URLs in code
A small helper keeps the three parts in one place and avoids the double-slash mistake:
import os
instance_url = os.environ["SF_INSTANCE_URL"].rstrip("/")
version = os.environ.get("SF_API_VERSION", "v60.0")
def sobject_url(sobject, record_id=None):
url = f"{instance_url}/services/data/{version}/sobjects/{sobject}"
if record_id:
url += f"/{record_id}"
return url
print(sobject_url("Account"))
print(sobject_url("Account", "001xx000003DGb2AAG"))Common URL mistakes
- Trailing slash on the instance URL. It produces
//services/data. - Missing
vin the version. Writev60.0, not60.0. - Using the login host.
login.salesforce.comissues tokens. Send API calls to the instance URL instead. - Wrong object name. A misspelled sObject returns
404withNOT_FOUND.
Exercise
Build the URL to read the Contact with ID 003xx000004TmiQAAS in an org at https://acme.my.salesforce.com, using API version v60.0. Which part of the URL should come from the token response?
Show answer
https://acme.my.salesforce.com/services/data/v60.0/sobjects/Contact/003xx000004TmiQAAS. The instance URL (https://acme.my.salesforce.com) should come from the instance_url value returned with the token.