Create Custom Tracking Domain method 🌐 Domains
Register a custom branded domain for click tracking, open tracking, or unsubscribe handling. By default, MailChannels uses shared domains for these links. Using a custom domain improves brand consistency by replacing shared domains with your own (e.g., click.example.com). Once registered, select the domain at send time using its name.
Before registration completes, two DNS records must be in place:
- A TXT record at
_mailchannels-verify.<hostname>containing the verification token (returned when DNS setup is required). - A CNAME record at
<hostname>pointing tolinks.mailchannels.net.
Usage
ts
import { MailChannelsClient, Domains } from 'mailchannels-sdk'
const mailchannels = new MailChannelsClient('your-api-key')
const domains = new Domains(mailchannels)
const { data, error } = await domains.customTracking.create(
'clickdemo',
'click.example.com',
'click'
)
if (error) {
throw new Error(error.message)
}
if (data.dnsSetupRequired) {
// DNS setup required fields available here
}
else {
// Domain fields available here
}ts
import { MailChannels } from 'mailchannels-sdk'
const mailchannels = new MailChannels('your-api-key')
const { data, error } = await mailchannels.domains.customTracking.create(
'clickdemo',
'click.example.com',
'click'
)
if (error) {
throw new Error(error.message)
}
if (data.dnsSetupRequired) {
// DNS setup required fields available here
}
else {
// Domain fields available here
}Params
namestringrequired: A unique label used to select this domain at message send time.IMPORTANT
Maximum length is
64characters. Must match the pattern^[a-z0-9-]+$.hostnamestringrequired: The hostname to register as a custom tracking domain. The hostname must have a CNAME record pointing tolinks.mailchannels.net.scopeDomainsCustomTrackingScoperequired: The event type this domain handles (click,open,unsubscribe).
Response
dataDomainsCustomTrackingWithDnsSetupRequired | nullnullable: The created domain or DNS verification info when pending.- When DNS setup is complete:
dnsSetupRequiredfalseguaranteed: Whether DNS setup is required for this domain.namestringguaranteed: The label for this custom tracking domain.hostnamestringguaranteed: The registered domain hostname.scopeDomainsCustomTrackingScopeguaranteed: The event type this domain handles (click,open,unsubscribe).status"active" | "disabled"guaranteed: Current status of the custom tracking domain.createdAtstringguaranteed: ISO 8601 timestamp when the domain was registered.
- When DNS setup is required:
dnsSetupRequiredtrueguaranteed: Whether DNS setup is required for this domain.tokenstringoptional: UUID v4 nonce; also the TXT record value to set. Present only when TXT ownership verification is pending.txtRecordNamestringoptional: Fully-qualified DNS TXT record name to add. Present only when TXT ownership verification is pending.txtRecordValuestringoptional: Value for the DNS TXT record. Present only when TXT ownership verification is pending.instructionsstringoptional: Human-readable guidance for the DNS records that must be in place before retrying.
- When DNS setup is complete:
errorErrorResponse | nullnullable: Error information if the operation failed.messagestringguaranteed: A human-readable description of the error.statusCodenumber | nullnullable: The HTTP status code from the API, ornullif the error is not related to an HTTP request. This field is intended for diagnostic use only and should not be relied upon.typestringguaranteed: A string identifier for the type of error. This field is intended for diagnostic use only and should not be relied upon.responseRecord<string, unknown> | nullnullable: An object containing the response, if available. This field may benullif no response is available or if the error is not related to an HTTP request or if the response is not a JSON object.
Type declarations
Signature
ts
async function create (name: string, hostname: string, scope: DomainsCustomTrackingScope): Promise<DomainsCustomTrackingCreateResponse>Response type declarations
ts
interface ErrorResponse {
message: string;
statusCode: number | null;
type: ErrorType;
response: Record<string, unknown> | null;
}ts
type DataResponse<T> = {
data: T;
error: null;
} | {
data: null;
error: ErrorResponse;
};Custom Tracking Domain type declarations
ts
type DomainsCustomTrackingScope = "click" | "open" | "unsubscribe";ts
interface DomainsCustomTrackingDomain {
name: string;
hostname: string;
scope: DomainsCustomTrackingScope;
status: "active" | "disabled";
createdAt: string;
}Create Custom Tracking Domain type declarations
ts
interface DomainsCustomTrackingDnsSetupRequired {
token?: string;
txtRecordName?: string;
txtRecordValue?: string;
instructions?: string;
}ts
type DomainsCustomTrackingWithDnsSetupRequired<T extends 202 | 201 | 200 | undefined = undefined> =
T extends undefined
? | DomainsCustomTrackingDomain & { dnsSetupRequired: false }
| DomainsCustomTrackingDnsSetupRequired & { dnsSetupRequired: true }
: T extends 202
? DomainsCustomTrackingDnsSetupRequired & { dnsSetupRequired: true }
: DomainsCustomTrackingDomain & { dnsSetupRequired: false };ts
type DomainsCustomTrackingCreateResponse = DataResponse<DomainsCustomTrackingWithDnsSetupRequired>;Source
Source • Playground • Docs • Tests