Namecheap
Namecheap
Connect agents to your Namecheap account to keep track of your domains and their DNS.
Keep an eye on your domains
- List your domains with their expiry dates, auto-renew and lock status.
- Find domains that are expiring soon or have expired.
- See a domain's nameservers, privacy protection and DNS settings.
- Check whether domain names are available to register.
Manage DNS records
- Read every DNS record of a domain that uses Namecheap DNS.
- With Write access, agents can add, update and remove records, for example to point a subdomain at a new host or add a verification TXT record. Every other record is kept, and each change is read back to confirm it was saved.
- Mail settings are protected: a domain using Namecheap email forwarding, Private Email or Gmail keeps its mail setting unless the change asks to switch it.
Agents can't register, renew or transfer domains, so nothing is ever charged to your Namecheap balance.
Setup instructions
Setup instructions
You need a Namecheap account with API access, and the IPv4 address Houston connects to Namecheap from. No developer app is required.
- Ask Houston for the IPv4 address its servers use to connect to other services. Namecheap answers only requests from addresses you whitelist.
- Sign in to Namecheap and open Profile > Tools. Under Business & Dev Tools, select Manage next to Namecheap API Access.
- Turn API access on and accept the terms. Namecheap enables it only for accounts that meet its requirements; if the switch isn't available, contact Namecheap support.
- Under Whitelisted IPs, select Edit, then Add IP. Enter Houston's address and select Save Changes.
- Copy your API key from the same page.
- In Houston, choose the access level. Keep Environment on Production.
- Enter your Namecheap username, the Whitelisted IP address from step 1, and the API key.
- Select Add connector.
Your API key stays on the Houston server.
Advanced
Access levels
Read lets agents read your domains and DNS records. Write also lets them add, update and remove DNS records. Neither level can register, renew or transfer domains, or change nameservers, contacts or privacy settings.
DNS changes only work for domains that use Namecheap's own nameservers. For a domain pointed at another DNS provider, change its records there.
Sandbox
To try the connector without touching real domains, create an account on sandbox.namecheap.com, turn on its API access and whitelist Houston's address there, then choose Sandbox as the Environment. Sandbox accounts and API keys are separate from production.
Troubleshooting
- "Invalid request IP" or a similar IP error. The address isn't whitelisted in Namecheap, or it isn't the one Houston connects from. Check both match the Whitelisted IP address setting.
- "API Key is invalid or API access has not been enabled". Copy the API key again, and check that API access is still on.
- A domain isn't found. Use the registered domain, such as
example.com, rather than a subdomain, and check that it belongs to this Namecheap account.
Files
Files
connector.luau
-- Namecheap XML API (https://www.namecheap.com/support/api/). Credentials stay on Houston. -- Arguments and results keep Namecheap's native field names. type Element = {name: string, attrs: {[string]: string}, children: {Element}, text: string} type Plain = {[string]: any} local PROVIDER = "Namecheap" local MAX_BODY = 4 * 1024 * 1024 local DEFAULT_TTL = 1799 local SERVER_ERRORS = {["3050900"] = true, ["5019169"] = true, ["5050169"] = true, ["5050900"] = true} local LIST_TYPES = {ALL = true, EXPIRING = true, EXPIRED = true} local SORT_BY = {NAME = true, NAME_DESC = true, EXPIREDATE = true, EXPIREDATE_DESC = true, CREATEDATE = true, CREATEDATE_DESC = true} local RECORD_TYPES = {A = true, AAAA = true, ALIAS = true, CAA = true, CNAME = true, MX = true, MXE = true, NS = true, TXT = true, URL = true, URL301 = true, FRAME = true} local EMAIL_TYPES = {NONE = true, MXE = true, MX = true, FWD = true, OX = true, GMAIL = true} -- Record types whose addresses are host names or IPs, compared without case. local CASELESS = {A = true, AAAA = true, ALIAS = true, CNAME = true, MX = true, MXE = true, NS = true} -- XML -------------------------------------------------------------------- local ENTITIES = {amp = "&", lt = "<", gt = ">", quot = '"', apos = "'"} local function unescape(value: string): string if string.find(value, "&", 1, true) == nil then return value end local parts: {string} = {} local pos = 1 while true do local first, last, reference = string.find(value, "&([#%w]+);", pos) if first == nil or last == nil or reference == nil then table.insert(parts, string.sub(value, pos)) break end table.insert(parts, string.sub(value, pos, first - 1)) local replacement: string? = ENTITIES[reference] local hex = string.match(reference, "^#[xX](%x+)$") local decimal = string.match(reference, "^#(%d+)$") local code = if hex then tonumber(hex, 16) elseif decimal then tonumber(decimal) else nil if code and code <= 0x10FFFF then replacement = utf8.char(code) end table.insert(parts, replacement or string.sub(value, first, last)) pos = last + 1 end return table.concat(parts) end local function localName(name: string): string return string.match(name, ":([^:]+)$") or name end -- Parses one XML document into elements. Comments, processing instructions and DOCTYPE are skipped. local function parseXML(source: string): Element local root: Element? = nil local stack: {Element} = {} local pos = 1 local size = #source while pos <= size do local open = string.find(source, "<", pos, true) local current = stack[#stack] local textEnd = if open then open - 1 else size if current and textEnd >= pos then current.text ..= unescape(string.sub(source, pos, textEnd)) end if not open then break end if string.sub(source, open, open + 3) == "<!--" then local close = string.find(source, "-->", open + 4, true) assert(close, "unterminated comment") pos = close + 3 elseif string.sub(source, open, open + 8) == "<![CDATA[" then local close = string.find(source, "]]>", open + 9, true) assert(close and current, "invalid CDATA section") current.text ..= string.sub(source, open + 9, close - 1) pos = close + 3 elseif string.sub(source, open, open + 1) == "<?" then local close = string.find(source, "?>", open + 2, true) assert(close, "unterminated processing instruction") pos = close + 2 elseif string.sub(source, open, open + 1) == "<!" then local close = string.find(source, ">", open + 2, true) assert(close, "unterminated declaration") pos = close + 1 elseif string.sub(source, open, open + 1) == "</" then local close = string.find(source, ">", open + 2, true) assert(close, "unterminated end tag") local name = string.match(string.sub(source, open + 2, close - 1), "^%s*([^%s]+)%s*$") assert(current and name and localName(name) == current.name, "mismatched end tag") table.remove(stack) pos = close + 1 else -- Find the tag's end outside quoted attribute values, which may contain ">". local cursor = open + 1 local quote: string? = nil local close: number? = nil while cursor <= size do local found = string.find(source, if quote then quote else "[\"'>]", cursor, quote ~= nil) assert(found, "unterminated start tag") local char = string.sub(source, found, found) if quote then quote = nil elseif char == ">" then close = found break else quote = char end cursor = found + 1 end assert(close, "unterminated start tag") local inner = string.sub(source, open + 1, close - 1) local selfClosing = string.sub(inner, -1) == "/" if selfClosing then inner = string.sub(inner, 1, -2) end local name, rest = string.match(inner, "^([%w_:%.%-]+)(.*)$") assert(name and rest, "invalid start tag") local element: Element = {name = localName(name), attrs = {}, children = {}, text = ""} local cursorAttr = 1 while true do local first, last, key, _, value = string.find(rest, "^%s+([%w_:%.%-]+)%s*=%s*([\"'])(.-)%2", cursorAttr) if first == nil or last == nil or key == nil or value == nil then break end element.attrs[localName(key)] = unescape(value) cursorAttr = last + 1 end assert(string.match(string.sub(rest, cursorAttr), "^%s*$"), "invalid attributes") if current then table.insert(current.children, element) elseif root == nil then root = element else error("more than one root element") end if not selfClosing then table.insert(stack, element) end pos = close + 1 end end assert(root and #stack == 0, "incomplete document") return root end local function child(element: Element, name: string): Element? for _, node in element.children do if node.name == name then return node end end return nil end local function childrenNamed(element: Element, name: string): {Element} local found: {Element} = {} for _, node in element.children do if node.name == name then table.insert(found, node) end end return found end local function trim(value: string): string return (string.match(value, "^%s*(.-)%s*$")) :: string end -- Converts "true"/"false" to booleans and plain integers or decimals to numbers. local function scalar(value: string): any local lower = string.lower(value) if lower == "true" then return true elseif lower == "false" then return false elseif string.match(value, "^%-?%d+$") and #value <= 15 and not string.match(value, "^%-?0%d") then return tonumber(value) elseif string.match(value, "^%-?%d+%.%d+$") and #value <= 17 then return tonumber(value) end return value end local function attrs(element: Element, raw: {[string]: boolean}?): Plain local out: Plain = {} for key, value in element.attrs do out[key] = if raw and raw[key] then value else scalar(value) end return out end -- A generic element tree: attributes and text-only children become fields; repeated or listed names become arrays. local function plain(element: Element, arrays: {[string]: boolean}): Plain local out = attrs(element) local counts: {[string]: number} = {} for _, node in element.children do counts[node.name] = (counts[node.name] or 0) + 1 end for _, node in element.children do local value: any if #node.children == 0 and next(node.attrs) == nil then value = scalar(trim(node.text)) else value = plain(node, arrays) local text = trim(node.text) if text ~= "" then value.Value = scalar(text) end end if arrays[node.name] or counts[node.name] > 1 then if out[node.name] == nil then out[node.name] = json.decode("[]") end table.insert(out[node.name], value) else out[node.name] = value end end return out end -- Requests --------------------------------------------------------------- local function fail(operation: string, message: string, status: number?, retryable: boolean?): never return houston.fail({ operation = operation, layer = "upstream", upstream_status = status, retryable = retryable == true, message = message, }) end local function invalid(message: string): never error(message, 0) end local function clientIp(): string local ip = config.client_ip local parts: {string} = {} if type(ip) == "string" then local a, b, c, d = string.match(ip, "^(%d+)%.(%d+)%.(%d+)%.(%d+)$") if a and b and c and d then parts = {a, b, c, d} end end if #parts ~= 4 then invalid("The connection's Whitelisted IP address must be an IPv4 address such as 203.0.113.7") end for _, part in parts do local number = tonumber(part) if number == nil or number > 255 or (#part > 1 and string.sub(part, 1, 1) == "0") then invalid("The connection's Whitelisted IP address must be an IPv4 address such as 203.0.113.7") end end return ip end local function encodePairs(pairs: {{string}}): string local parts = {} for _, pair in pairs do table.insert(parts, url.encode(pair[1]) .. "=" .. url.encode(pair[2])) end return table.concat(parts, "&") end local function sortedPairs(params: {[string]: string}): {{string}} local keys = {} for key in params do table.insert(keys, key) end table.sort(keys) local out: {{string}} = {} for _, key in keys do table.insert(out, {key, params[key]}) end return out end -- Calls one command. Reads send everything in the query; writes send their fields as a form body. local function call(operation: string, command: string, params: {[string]: string}, form: {{string}}?): Element local username = config.username if type(username) ~= "string" or trim(username) == "" then invalid("The connection's Namecheap username is missing") end local ip = clientIp() local query = encodePairs({ {"ApiUser", trim(username)}, {"UserName", trim(username)}, {"ClientIp", ip}, {"Command", command}, }) local extra = encodePairs(sortedPairs(params)) if extra ~= "" then query ..= "&" .. extra end local target = "https://" .. config.api_host .. "/xml.response?" .. query local response = if form then await(http.request({ method = "POST", url = target, headers = {["Content-Type"] = {"application/x-www-form-urlencoded"}}, body = encodePairs(form), })) else await(http.request({method = "GET", url = target})) if response.statusCode ~= 200 then local snippet = await(response.body:readPrefix(512)) local hint = if response.statusCode == 405 then " (Namecheap's rate limit; wait before retrying)" else "" return fail(operation, `{PROVIDER} answered HTTP {response.statusCode}{hint}: {snippet}`, response.statusCode, response.statusCode == 405 or response.statusCode == 429 or response.statusCode >= 500) end local raw = await(response.body:readAll(MAX_BODY)) local ok, document = pcall(parseXML, raw) if not ok or document.name ~= "ApiResponse" then return fail(operation, `{PROVIDER} returned a response that is not its XML API format`, 200, false) end local errors = child(document, "Errors") local entries: {Element} = if errors then childrenNamed(errors, "Error") else {} if #entries > 0 then local messages = {} local retryable = false for _, entry in entries do local number = entry.attrs.Number or "?" retryable = retryable or SERVER_ERRORS[number] == true table.insert(messages, `error {number}: {trim(entry.text)}`) end local message = `{PROVIDER} {table.concat(messages, "; ")}` if string.find(string.lower(message), "%f[%a]ip%f[%A]") then message ..= `. Check that {ip} is whitelisted in Namecheap's API settings and is the address Houston connects from` end return fail(operation, message, 200, retryable) end if document.attrs.Status ~= "OK" then return fail(operation, `{PROVIDER} returned status {document.attrs.Status or "unknown"} without an error`, 200, false) end local result = child(document, "CommandResponse") if result == nil then return fail(operation, `{PROVIDER} returned no CommandResponse`, 200, false) end return result end local function requireResult(operation: string, response: Element, name: string): Element local result = child(response, name) if result == nil then return fail(operation, `{PROVIDER} returned no {name}`, 200, false) end return result end -- Arguments -------------------------------------------------------------- local function options(value: any, field: string, allowed: {[string]: boolean}): Plain if value == nil then return {} end if type(value) ~= "table" then invalid(field .. " must be a table") end for key in value do if type(key) ~= "string" or not allowed[key] then invalid(`{field} has an unknown field {tostring(key)}`) end end return value end local function text(value: any, field: string, maxLength: number): string if type(value) ~= "string" or trim(value) == "" then invalid(field .. " must be a nonempty string") end if #value > maxLength then invalid(`{field} must be at most {maxLength} characters`) end return trim(value) end local function integer(value: any, field: string, min: number, max: number): number if type(value) ~= "number" or value ~= math.floor(value) or value < min or value > max then invalid(`{field} must be an integer from {min} to {max}`) end return value end -- A registered domain such as example.com or example.co.uk, split at its first dot. local function domainName(value: any): (string, string, string) local domain = string.lower(text(value, "domain", 253)) if string.sub(domain, -1) == "." then domain = string.sub(domain, 1, -2) end if not string.match(domain, "^[%w%-]+%.[%w%-%.]+$") or string.find(domain, "..", 1, true) or string.sub(domain, -1) == "." then invalid("domain must be a registered domain name such as example.com, without http:// or a path") end local sld, tld = string.match(domain, "^([^%.]+)%.(.+)$") return domain, sld :: string, tld :: string end -- Reads ------------------------------------------------------------------ local function listDomains(opts: Plain?): Plain local input = options(opts, "options", {ListType = true, SearchTerm = true, Page = true, PageSize = true, SortBy = true}) local params: {[string]: string} = {} if input.ListType ~= nil then if type(input.ListType) ~= "string" or not LIST_TYPES[input.ListType] then invalid("ListType must be ALL, EXPIRING or EXPIRED") end params.ListType = input.ListType end if input.SortBy ~= nil then if type(input.SortBy) ~= "string" or not SORT_BY[input.SortBy] then invalid("SortBy must be NAME, NAME_DESC, EXPIREDATE, EXPIREDATE_DESC, CREATEDATE or CREATEDATE_DESC") end params.SortBy = input.SortBy end if input.SearchTerm ~= nil then params.SearchTerm = text(input.SearchTerm, "SearchTerm", 70) end if input.Page ~= nil then params.Page = tostring(integer(input.Page, "Page", 1, 100000)) end if input.PageSize ~= nil then params.PageSize = tostring(integer(input.PageSize, "PageSize", 10, 100)) end local response = call("listDomains", "namecheap.domains.getList", params) local result = requireResult("listDomains", response, "DomainGetListResult") local domains = json.decode("[]") for _, node in childrenNamed(result, "Domain") do table.insert(domains, attrs(node, {Name = true, User = true})) end local paging = child(response, "Paging") local out: Plain = {Domains = domains} if paging then for _, field in {"TotalItems", "CurrentPage", "PageSize"} do local node = child(paging, field) if node then out[field] = tonumber(trim(node.text)) end end end return out end local function getDomain(domain: string): Plain local name = domainName(domain) local response = call("getDomain", "namecheap.domains.getInfo", {DomainName = name}) return plain(requireResult("getDomain", response, "DomainGetInfoResult"), {Nameserver = true, Rights = true}) end local function checkAvailability(domains: {string}): Plain if type(domains) ~= "table" or #domains == 0 or #domains > 50 then invalid("domains must be an array of 1 to 50 domain names") end local names = {} for index, value in domains do if type(index) ~= "number" then invalid("domains must be an array of 1 to 50 domain names") end table.insert(names, (domainName(value))) end local response = call("checkAvailability", "namecheap.domains.check", {DomainList = table.concat(names, ",")}) local results = json.decode("[]") for _, node in childrenNamed(response, "DomainCheckResult") do table.insert(results, attrs(node, {Domain = true})) end return {Results = results} end local function getNameservers(domain: string): Plain local _, sld, tld = domainName(domain) local response = call("getNameservers", "namecheap.domains.dns.getList", {SLD = sld, TLD = tld}) local result = requireResult("getNameservers", response, "DomainDNSGetListResult") local out = attrs(result, {Domain = true}) local servers = json.decode("[]") for _, node in childrenNamed(result, "Nameserver") do table.insert(servers, trim(node.text)) end out.Nameservers = servers return out end local HOST_TEXT = {Name = true, Type = true, Address = true, AssociatedAppTitle = true, FriendlyName = true} local function readHosts(operation: string, sld: string, tld: string): Plain local response = call(operation, "namecheap.domains.dns.getHosts", {SLD = sld, TLD = tld}) local result = requireResult(operation, response, "DomainDNSGetHostsResult") local out = attrs(result, {Domain = true, EmailType = true}) local hosts = json.decode("[]") for _, node in result.children do if string.lower(node.name) == "host" then table.insert(hosts, attrs(node, HOST_TEXT)) end end out.Hosts = hosts return out end local function getDnsRecords(domain: string): Plain local _, sld, tld = domainName(domain) return readHosts("getDnsRecords", sld, tld) end -- DNS changes ------------------------------------------------------------ type Record = {Name: string, Type: string, Address: string, MXPref: number?, TTL: number?} local function normName(name: string): string local value = string.lower(trim(name)) return if value == "" then "@" else value end local function normAddress(recordType: string, address: string): string local value = trim(address) if CASELESS[recordType] then value = string.lower(value) if string.sub(value, -1) == "." and #value > 1 then value = string.sub(value, 1, -2) end end return value end -- Identity of a record: TTL is a property, not part of it. local function identity(record: Record): string local pref = if record.Type == "MX" then tostring(record.MXPref or "") else "" return table.concat({normName(record.Name), record.Type, normAddress(record.Type, record.Address), pref}, "\0") end local function survives(recordType: string, emailType: string): boolean if recordType == "MX" then return emailType == "MX" elseif recordType == "MXE" then return emailType == "MXE" or emailType == "MX" end return true end local function isMail(recordType: string): boolean return recordType == "MX" or recordType == "MXE" end local function newRecord(value: any, index: number): Record local field = `add[{index}]` local input = options(value, field, {Name = true, Type = true, Address = true, MXPref = true, TTL = true}) local recordType = string.upper(text(input.Type, field .. ".Type", 10)) if not RECORD_TYPES[recordType] then invalid(`{field}.Type must be one of A, AAAA, ALIAS, CAA, CNAME, MX, MXE, NS, TXT, URL, URL301, FRAME`) end local record: Record = { Name = normName(text(input.Name, field .. ".Name", 255)), Type = recordType, Address = text(input.Address, field .. ".Address", 2048), } if input.TTL ~= nil then record.TTL = integer(input.TTL, field .. ".TTL", 60, 60000) end if recordType == "MX" then record.MXPref = integer(input.MXPref, field .. ".MXPref", 0, 65535) elseif input.MXPref ~= nil then invalid(`{field}.MXPref is only for MX records`) end if (recordType == "URL" or recordType == "URL301" or recordType == "FRAME") and not string.match(record.Address, "^%a[%w+.%-]*://") then invalid(`{field}.Address must start with a protocol such as https:// for {recordType} records`) end return record end type Selector = {Name: string?, Type: string?, Address: string?} local function selector(value: any, index: number): Selector local field = `remove[{index}]` local input = options(value, field, {Name = true, Type = true, Address = true}) local out: Selector = {} if input.Name ~= nil then out.Name = normName(text(input.Name, field .. ".Name", 255)) end if input.Type ~= nil then out.Type = string.upper(text(input.Type, field .. ".Type", 10)) end if input.Address ~= nil then out.Address = text(input.Address, field .. ".Address", 2048) end if out.Name == nil and out.Type == nil and out.Address == nil then invalid(`{field} must name at least one of Name, Type and Address`) end return out end local function matches(rule: Selector, record: Record): boolean if rule.Name and rule.Name ~= normName(record.Name) then return false end if rule.Type and rule.Type ~= record.Type then return false end if rule.Address and normAddress(record.Type, rule.Address) ~= normAddress(record.Type, record.Address) then return false end return true end local function list(value: any, field: string): {any} if value == nil then return {} end if type(value) ~= "table" then invalid(field .. " must be an array") end local count = 0 for key in value do count += 1 if type(key) ~= "number" then invalid(field .. " must be an array") end end if count ~= #value then invalid(field .. " must be an array without gaps") end return value end local function fromHost(host: Plain): Record local recordType = string.upper(tostring(host.Type or "")) local ttl = if type(host.TTL) == "number" then host.TTL else nil return { Name = tostring(host.Name or "@"), Type = recordType, Address = tostring(host.Address or ""), MXPref = if recordType == "MX" and type(host.MXPref) == "number" then host.MXPref else nil, TTL = ttl, } end local function recordList(records: {Record}): {Plain} local out = json.decode("[]") for _, record in records do table.insert(out, {Name = record.Name, Type = record.Type, Address = record.Address, MXPref = record.MXPref, TTL = record.TTL}) end return out end local function stateKey(record: Record): string return identity(record) .. "\0" .. tostring(record.TTL or DEFAULT_TTL) end local function sameState(a: {Record}, b: {Record}): boolean local counts: {[string]: number} = {} for _, record in a do local key = stateKey(record) counts[key] = (counts[key] or 0) + 1 end for _, record in b do local key = stateKey(record) if not counts[key] or counts[key] == 0 then return false end counts[key] -= 1 end for _, count in counts do if count ~= 0 then return false end end return true end local function changeDnsRecords(domain: string, change: Plain): Plain local name, sld, tld = domainName(domain) local input = options(change, "change", {add = true, remove = true, EmailType = true}) local adds: {Record} = {} for index, value in list(input.add, "add") do table.insert(adds, newRecord(value, index)) end local removes: {Selector} = {} for index, value in list(input.remove, "remove") do table.insert(removes, selector(value, index)) end if #adds == 0 and #removes == 0 then invalid("change must add or remove at least one record") end local requested: string? = nil if input.EmailType ~= nil then if type(input.EmailType) ~= "string" or not EMAIL_TYPES[input.EmailType] then invalid("EmailType must be NONE, MXE, MX, FWD, OX or GMAIL") end requested = input.EmailType end local zone = readHosts("changeDnsRecords", sld, tld) if zone.IsUsingOurDNS == false then invalid(`{name} uses custom nameservers, so Namecheap's DNS records for it are not served; change records at its DNS provider`) end local current = if type(zone.EmailType) == "string" and zone.EmailType ~= "" then string.upper(zone.EmailType) else "NONE" -- Remove: every selector must match at least one record, so a typo cannot pass silently. local kept: {Record} = {} local removed: {Record} = {} local used: {boolean} = {} for _, host in zone.Hosts do local record = fromHost(host) local hit = false for index, rule in removes do if matches(rule, record) then used[index] = true hit = true end end table.insert(if hit then removed else kept, record) end for index in removes do if not used[index] then invalid(`remove[{index}] matched no record in {name}; read the records with getDnsRecords first`) end end -- Add: an existing record with the same identity is updated in place (its TTL), not duplicated. local added, updated, unchanged = 0, 0, 0 local addedMail = false for _, record in adds do local key = identity(record) local existing: Record? = nil for _, candidate in kept do if identity(candidate) == key then existing = candidate break end end if existing then if record.TTL ~= nil and (existing.TTL or DEFAULT_TTL) ~= record.TTL then existing.TTL = record.TTL updated += 1 else unchanged += 1 end else table.insert(kept, record) added += 1 addedMail = addedMail or isMail(record.Type) end end local final = kept if added == 0 and updated == 0 and #removed == 0 then return {Domain = name, Changed = false, Added = 0, Updated = 0, Removed = 0, Unchanged = unchanged, EmailType = current, Verified = true, Hosts = recordList(final)} end -- Mail mode: MX rows survive only in MX mode, MXE rows in MXE or MX. A preset is never left silently. local hasMX, hasMXE = false, false for _, record in final do hasMX = hasMX or record.Type == "MX" hasMXE = hasMXE or record.Type == "MXE" end local send: string? = requested if send == nil then local derived: string? = nil if addedMail then derived = if hasMX then "MX" elseif hasMXE then "MXE" else nil end local recordDriven = current == "NONE" or current == "MX" or current == "MXE" if derived and derived ~= current and not recordDriven then invalid(`{name} uses Namecheap's {current} mail setting; these records need EmailType {derived}, which replaces it. Pass EmailType = "{derived}" to switch`) end send = derived or (if hasMX or hasMXE then current else nil) end for _, record in final do if isMail(record.Type) and not survives(record.Type, send or current) then local need = if record.Type == "MX" then "MX" else "MXE or MX" invalid(`{record.Type} record {record.Name} -> {record.Address} needs EmailType {need}, but the write would use {send or current}; Namecheap would drop it. Remove it or pass a matching EmailType`) end end local form: {{string}} = {{"SLD", sld}, {"TLD", tld}} if send then table.insert(form, {"EmailType", send}) end for index, record in final do table.insert(form, {"HostName" .. index, record.Name}) table.insert(form, {"RecordType" .. index, record.Type}) table.insert(form, {"Address" .. index, record.Address}) if record.Type == "MX" and record.MXPref then table.insert(form, {"MXPref" .. index, tostring(record.MXPref)}) end if record.TTL then table.insert(form, {"TTL" .. index, tostring(record.TTL)}) end end local response = call("changeDnsRecords", "namecheap.domains.dns.setHosts", {}, form) local result = requireResult("changeDnsRecords", response, "DomainDNSSetHostsResult") if scalar(result.attrs.IsSuccess or "") ~= true then return fail("changeDnsRecords", `{PROVIDER} did not apply the change to {name} (IsSuccess was not true); the zone should be unchanged, so read it before retrying`, 200, false) end local out: Plain = {Domain = name, Changed = true, Added = added, Updated = updated, Removed = #removed, Unchanged = unchanged, EmailType = send or current} local ok, after = pcall(readHosts, "changeDnsRecords", sld, tld) if not ok then out.Verified = false out.VerifyError = "The change was applied, but reading the records back failed; read them with getDnsRecords" out.Hosts = recordList(final) return out end local written: {Record} = {} for _, host in after.Hosts do table.insert(written, fromHost(host)) end out.Verified = sameState(final, written) if not out.Verified then out.VerifyError = "The records read back differ from those written, possibly from a concurrent change; review them before changing again" end out.Hosts = recordList(written) return out end -- Exports ---------------------------------------------------------------- local exports: {[string]: any} = { listDomains = listDomains, getDomain = getDomain, checkAvailability = checkAvailability, getNameservers = getNameservers, getDnsRecords = getDnsRecords, } if houston.allows("data:write") then exports.changeDnsRecords = changeDnsRecords end local HELP: {[string]: string} = { listDomains = [[listDomains(options?) -> {Domains, TotalItems, CurrentPage, PageSize} Lists the account's domains, one page at a time. options: ListType ("ALL", "EXPIRING", "EXPIRED"), SearchTerm (up to 70 characters), Page (from 1), PageSize (10 to 100, default 20), SortBy ("NAME", "NAME_DESC", "EXPIREDATE", "EXPIREDATE_DESC", "CREATEDATE", "CREATEDATE_DESC"). Each domain is {ID, Name, User, Created, Expires, IsExpired, IsLocked, AutoRenew, WhoisGuard, IsPremium, IsOurDNS}; dates are Namecheap's MM/DD/YYYY text. Request the next Page while CurrentPage * PageSize < TotalItems. Example: namecheap.listDomains({ListType = "EXPIRING", SortBy = "EXPIREDATE"})]], getDomain = [[getDomain(domain) -> DomainGetInfoResult Details of one domain: Status, ID, DomainName, OwnerName, IsOwner, IsPremium, DomainDetails {CreatedDate, ExpiredDate, NumYears}, Whoisguard, PremiumDnsSubscription, DnsDetails {ProviderType, IsUsingOurDNS, HostCount, EmailType, Nameserver = {...}}, Modificationrights {All, Rights}. Auto-renew and lock state come from listDomains instead. Example: namecheap.getDomain("example.com")]], checkAvailability = [[checkAvailability(domains) -> {Results} Checks whether 1 to 50 domain names can be registered. Each result is {Domain, Available, IsPremiumName, PremiumRegistrationPrice, PremiumRenewalPrice, PremiumRestorePrice, PremiumTransferPrice, IcannFee, EapFee}. It registers nothing. Example: namecheap.checkAvailability({"example.com", "example.io"})]], getNameservers = [[getNameservers(domain) -> {Domain, IsUsingOurDNS, IsPremiumDNS, IsUsingFreeDNS, Nameservers} The domain's nameservers. When IsUsingOurDNS is false, DNS records are served elsewhere and the records shown by getDnsRecords are not in use. Example: namecheap.getNameservers("example.com")]], getDnsRecords = [[getDnsRecords(domain) -> {Domain, EmailType, IsUsingOurDNS, Hosts} The domain's DNS records in Namecheap DNS. Each host is {HostId, Name, Type, Address, MXPref, TTL, AssociatedAppTitle, FriendlyName, IsActive, IsDDNSEnabled}; Name is "@" for the domain itself; MXPref only matters for MX records. EmailType is the mail setting: NONE, MX (custom MX records), MXE (mail forwarding to an IP), FWD (Namecheap email forwarding), OX (Private Email) or GMAIL. Pass the registered domain, such as example.com or example.co.uk, not a subdomain. Example: namecheap.getDnsRecords("example.com")]], changeDnsRecords = [[changeDnsRecords(domain, change) -> {Domain, Changed, Added, Updated, Removed, Unchanged, EmailType, Verified, VerifyError?, Hosts} Adds and removes DNS records while keeping every other record. Namecheap only replaces a domain's whole record set, so this reads the records, applies the change, writes the full set, then reads it back and sets Verified. change = { add = {{Name, Type, Address, MXPref?, TTL?}, ...}, -- MXPref required for MX remove = {{Name?, Type?, Address?}, ...}, -- at least one field each EmailType = "MX"?, -- only to switch mail mode } Type is A, AAAA, ALIAS, CAA, CNAME, MX, MXE, NS, TXT, URL, URL301 or FRAME. Name is the host part ("@", "www", "_dmarc"). TTL is 60 to 60000 seconds, default Namecheap's automatic 1799. URL, URL301 and FRAME addresses need a protocol such as https://. For CAA, copy the Address format getDnsRecords shows. Removals happen before additions, so remove and add the same Name and Type to replace a record. Each remove rule must match at least one record, or nothing is written. Adding a record that already exists changes only its TTL. Mail: MX records are kept only in EmailType MX and MXE records in MXE or MX; adding them switches a NONE, MX or MXE domain automatically. A domain on the FWD, OX or GMAIL mail setting is never switched unless EmailType is given, because that changes where its mail goes. The change fails when the domain uses other nameservers. A failed or interrupted call may already have applied; read the records before retrying. Example: namecheap.changeDnsRecords("example.com", { remove = {{Name = "www", Type = "CNAME"}}, add = {{Name = "www", Type = "CNAME", Address = "example.netlify.app", TTL = 300}}, })]], } function exports.help(): string local sections = { "namecheap: domains and DNS records of one Namecheap account. Connector calls return values directly; do not await them. " .. "Errors carry Namecheap's error number and message. Namecheap accepts calls only from the connection's whitelisted IP address.", } for _, name in {"listDomains", "getDomain", "checkAvailability", "getNameservers", "getDnsRecords", "changeDnsRecords"} do if exports[name] then table.insert(sections, HELP[name]) end end return table.concat(sections, "\n\n") end return exportshouston.json
{ "schema_version": 2, "name": "Namecheap", "files": [ "connector.luau" ], "icon": "icon.svg", "access": [ { "name": "data", "label": "Access", "description": "Write also lets agents add, update and remove DNS records.", "levels": [ { "value": "read", "label": "Read" }, { "value": "write", "label": "Write" } ], "default": "read" } ], "config": { "api_host": { "default": "api.namecheap.com", "description": "Use Sandbox only with an account created on sandbox.namecheap.com.", "label": "Environment", "options": [ { "label": "Production", "value": "api.namecheap.com" }, { "label": "Sandbox", "value": "api.sandbox.namecheap.com" } ], "order": 1, "required": true, "type": "string" }, "username": { "description": "The username you sign in to Namecheap with.", "label": "Namecheap username", "max_length": 64, "order": 2, "required": true, "type": "string" }, "client_ip": { "description": "The IPv4 address Houston connects to Namecheap from. Whitelist the same address in Namecheap's API settings.", "label": "Whitelisted IP address", "max_length": 15, "order": 3, "placeholder": "203.0.113.7", "required": true, "type": "string" } }, "auth": { "token": { "type": "manual", "label": "API key", "config": { "token": { "label": "API key", "required": true, "type": "secret" } } } }, "proxy": [ { "match": { "protocol": "http", "host": [ "{{config.api_host}}" ], "method": [ "GET" ], "path": [ "/xml.response" ] }, "action": { "headers": { "remove": [ "Authorization", "Cookie", "X-HTTP-Method-Override", "X-HTTP-Method", "X-Method-Override" ] }, "query": { "ApiKey": "{{auth.token.token}}" } } }, { "if": "{{ allows access.data \"write\" }}", "match": { "protocol": "http", "host": [ "{{config.api_host}}" ], "method": [ "POST" ], "path": [ "/xml.response" ] }, "action": { "headers": { "remove": [ "Authorization", "Cookie", "X-HTTP-Method-Override", "X-HTTP-Method", "X-Method-Override" ] }, "query": { "ApiKey": "{{auth.token.token}}" } } } ] }icon.svg
README.md
--- description: Let agents look up your Namecheap domains and DNS, and change DNS records when you allow it. --- # Namecheap Connect agents to your Namecheap account to keep track of your domains and their DNS. ## Keep an eye on your domains - List your domains with their expiry dates, auto-renew and lock status. - Find domains that are expiring soon or have expired. - See a domain's nameservers, privacy protection and DNS settings. - Check whether domain names are available to register. ## Manage DNS records - Read every DNS record of a domain that uses Namecheap DNS. - With **Write** access, agents can add, update and remove records, for example to point a subdomain at a new host or add a verification TXT record. Every other record is kept, and each change is read back to confirm it was saved. - Mail settings are protected: a domain using Namecheap email forwarding, Private Email or Gmail keeps its mail setting unless the change asks to switch it. Agents can't register, renew or transfer domains, so nothing is ever charged to your Namecheap balance.SETUP.md
You need a Namecheap account with API access, and the IPv4 address Houston connects to Namecheap from. No developer app is required. 1. Ask Houston for the IPv4 address its servers use to connect to other services. Namecheap answers only requests from addresses you whitelist. 2. Sign in to [Namecheap](https://www.namecheap.com) and open **Profile** > **Tools**. Under **Business & Dev Tools**, select **Manage** next to **Namecheap API Access**. 3. Turn API access on and accept the terms. Namecheap enables it only for accounts that meet its [requirements](https://www.namecheap.com/support/api/intro/); if the switch isn't available, contact Namecheap support. 4. Under **Whitelisted IPs**, select **Edit**, then **Add IP**. Enter Houston's address and select **Save Changes**. 5. Copy your **API key** from the same page. 6. In Houston, choose the access level. Keep **Environment** on **Production**. 7. Enter your **Namecheap username**, the **Whitelisted IP address** from step 1, and the **API key**. 8. Select **Add connector**. Your API key stays on the Houston server. ## Advanced ### Access levels **Read** lets agents read your domains and DNS records. **Write** also lets them add, update and remove DNS records. Neither level can register, renew or transfer domains, or change nameservers, contacts or privacy settings. DNS changes only work for domains that use Namecheap's own nameservers. For a domain pointed at another DNS provider, change its records there. ### Sandbox To try the connector without touching real domains, create an account on [sandbox.namecheap.com](https://www.sandbox.namecheap.com), turn on its API access and whitelist Houston's address there, then choose **Sandbox** as the **Environment**. Sandbox accounts and API keys are separate from production. ### Troubleshooting - **"Invalid request IP" or a similar IP error.** The address isn't whitelisted in Namecheap, or it isn't the one Houston connects from. Check both match the **Whitelisted IP address** setting. - **"API Key is invalid or API access has not been enabled".** Copy the API key again, and check that API access is still on. - **A domain isn't found.** Use the registered domain, such as `example.com`, rather than a subdomain, and check that it belongs to this Namecheap account.tests
change_dns.luau
-- changeDnsRecords reads the zone, writes the complete record set by POST, reads it back, and guards mail settings. local function decode(value: string): string value = string.gsub(value, "+", " ") return (string.gsub(value, "%%(%x%x)", function(hex) return string.char(tonumber(hex, 16)) end)) end local function form(body: string): ({{string}}, {[string]: string}) local pairs, map = {}, {} for part in string.gmatch(body, "[^&]+") do local key, value = string.match(part, "^([^=]*)=(.*)$") table.insert(pairs, {decode(key), decode(value)}) map[decode(key)] = decode(value) end return pairs, map end local function attr(value: string): string return (string.gsub(string.gsub(string.gsub(value, "&", "&"), '"', """), "<", "<")) end local function hostsXML(): string local rows = {} for index, host in zone.hosts do table.insert(rows, string.format('<host HostId="%d" Name="%s" Type="%s" Address="%s" MXPref="%d" TTL="%d" IsActive="true" />', index, attr(host.Name), host.Type, attr(host.Address), host.MXPref or 10, host.TTL or 1799)) end return '<?xml version="1.0" encoding="utf-8"?><ApiResponse Status="OK"><Errors /><CommandResponse Type="namecheap.domains.dns.getHosts">' .. string.format('<DomainDNSGetHostsResult Domain="example.com" EmailType="%s" IsUsingOurDNS="%s">', zone.emailType, tostring(zone.ourDNS)) .. table.concat(rows) .. '</DomainDNSGetHostsResult></CommandResponse></ApiResponse>' end local BASE = "https://api.namecheap.com/xml.response?ApiUser=ada&UserName=ada&ClientIp=203.0.113.7" return { scenario = {publisher = {}, auth_method = "token", auth_config = {token = "fixture-key"}, config = {api_host = "api.namecheap.com", username = "ada", client_ip = "203.0.113.7"}}, access = {data = "write"}, configure = function() requests = {} zone = {} http = {request = function(req) table.insert(requests, req) if req.method == "GET" then assert(string.find(req.url, "&Command=namecheap.domains.dns.getHosts&SLD=example&TLD=com", 1, true), req.url) return fixture.operation({statusCode = 200, headers = {}, body = fixture.reader(hostsXML())}) end assert(req.method == "POST" and req.url == BASE .. "&Command=namecheap.domains.dns.setHosts", req.url) assert(req.headers["Content-Type"][1] == "application/x-www-form-urlencoded") local _, fields = form(req.body) if zone.reject then return fixture.operation({statusCode = 200, headers = {}, body = fixture.reader('<ApiResponse Status="OK"><Errors /><CommandResponse>' .. '<DomainDNSSetHostsResult Domain="example.com" IsSuccess="false" /></CommandResponse></ApiResponse>')}) end local written = {} local index = 1 while fields["HostName" .. index] do table.insert(written, {Name = fields["HostName" .. index], Type = fields["RecordType" .. index], Address = fields["Address" .. index], MXPref = tonumber(fields["MXPref" .. index]), TTL = tonumber(fields["TTL" .. index])}) index += 1 end zone.hosts = written if fields.EmailType then zone.emailType = fields.EmailType end if zone.tamper then table.insert(zone.hosts, {Name = "rogue", Type = "A", Address = "198.51.100.9"}) end return fixture.operation({statusCode = 200, headers = {}, body = fixture.reader('<ApiResponse Status="OK"><Errors /><CommandResponse>' .. '<DomainDNSSetHostsResult Domain="example.com" IsSuccess="true" /></CommandResponse></ApiResponse>')}) end} end, run = function(namecheap) assert(type(namecheap.changeDnsRecords) == "function" and type(namecheap.getDnsRecords) == "function") local function setZone(emailType, hosts, extra) zone = {emailType = emailType, ourDNS = true, hosts = hosts} for k, v in extra or {} do zone[k] = v end requests = {} end local function webZone() return { {Name = "@", Type = "A", Address = "192.0.2.10"}, {Name = "@", Type = "TXT", Address = "v=spf1 -all", TTL = 300}, {Name = "www", Type = "CNAME", Address = "old.example.net.", TTL = 300}, } end local function rejects(fn, text, expectedRequests) local ok, err = pcall(fn) assert(not ok and string.find(tostring(err), text, 1, true), "expected " .. text .. ", got " .. tostring(err)) assert(#requests == expectedRequests, "expected " .. expectedRequests .. " requests, got " .. #requests) for _, req in requests do assert(req.method == "GET", "a refused change must not write") end end -- Replace a record and add another: every other record is written back unchanged. setZone("NONE", webZone()) local result = namecheap.changeDnsRecords("example.com", { remove = {{Name = "WWW", Type = "cname"}}, add = { {Name = "www", Type = "CNAME", Address = "example.netlify.app", TTL = 300}, {Name = "_acme-challenge", Type = "TXT", Address = "token &=+ x"}, }, }) assert(#requests == 3 and requests[1].method == "GET" and requests[2].method == "POST" and requests[3].method == "GET") local sent = form(requests[2].body) local expected = { {"SLD", "example"}, {"TLD", "com"}, {"HostName1", "@"}, {"RecordType1", "A"}, {"Address1", "192.0.2.10"}, {"TTL1", "1799"}, {"HostName2", "@"}, {"RecordType2", "TXT"}, {"Address2", "v=spf1 -all"}, {"TTL2", "300"}, {"HostName3", "www"}, {"RecordType3", "CNAME"}, {"Address3", "example.netlify.app"}, {"TTL3", "300"}, {"HostName4", "_acme-challenge"}, {"RecordType4", "TXT"}, {"Address4", "token &=+ x"}, } assert(#sent == #expected, "sent " .. #sent .. " fields: " .. requests[2].body) for i, pair in expected do assert(sent[i][1] == pair[1] and sent[i][2] == pair[2], "field " .. i .. ": " .. sent[i][1] .. "=" .. sent[i][2]) end assert(string.find(requests[2].body, "Address4=token%20%26%3D%2B%20x", 1, true), requests[2].body) assert(not string.find(requests[2].body, "EmailType", 1, true), "a change without mail records keeps the mail setting") assert(not string.find(requests[2].body, "MXPref", 1, true), "MXPref is sent only for MX records") assert(result.Changed == true and result.Added == 2 and result.Removed == 1 and result.Updated == 0) assert(result.Verified == true and result.VerifyError == nil and result.EmailType == "NONE" and #result.Hosts == 4) -- Adding a record that already exists writes nothing. setZone("NONE", webZone()) result = namecheap.changeDnsRecords("example.com", {add = {{Name = "www", Type = "CNAME", Address = "OLD.example.net"}}}) assert(#requests == 1 and result.Changed == false and result.Unchanged == 1 and result.Added == 0) -- Adding an existing record with a new TTL updates it in place. setZone("NONE", webZone()) result = namecheap.changeDnsRecords("example.com", {add = {{Name = "@", Type = "A", Address = "192.0.2.10", TTL = 600}}}) local _, fields = form(requests[2].body) assert(result.Updated == 1 and result.Added == 0 and fields.TTL1 == "600" and fields.HostName4 == nil) -- A remove rule that matches nothing stops the change before any write. setZone("NONE", webZone()) rejects(function() namecheap.changeDnsRecords("example.com", {remove = {{Name = "api"}}}) end, "remove[1] matched no record", 1) -- Adding MX records to a NONE zone switches it to MX mail and sends MXPref. setZone("NONE", webZone()) result = namecheap.changeDnsRecords("example.com", {add = {{Name = "@", Type = "MX", Address = "mx.example.net", MXPref = 10}}}) _, fields = form(requests[2].body) assert(fields.EmailType == "MX" and fields.RecordType4 == "MX" and fields.MXPref4 == "10" and result.EmailType == "MX" and result.Verified) -- A Gmail preset is never switched silently, but can be switched on request. setZone("GMAIL", webZone()) rejects(function() namecheap.changeDnsRecords("example.com", {add = {{Name = "@", Type = "MX", Address = "mx.example.net", MXPref = 10}}}) end, 'Pass EmailType = "MX"', 1) setZone("GMAIL", webZone()) result = namecheap.changeDnsRecords("example.com", {EmailType = "MX", add = {{Name = "@", Type = "MX", Address = "mx.example.net", MXPref = 10}}}) _, fields = form(requests[2].body) assert(fields.EmailType == "MX" and result.EmailType == "MX") -- A preset zone keeps its preset on an unrelated change. setZone("FWD", webZone()) namecheap.changeDnsRecords("example.com", {add = {{Name = "blog", Type = "A", Address = "192.0.2.20"}}}) assert(not string.find(requests[2].body, "EmailType", 1, true)) -- On an MX zone, existing MX records are kept by sending the current mode. local mailZone = webZone() table.insert(mailZone, {Name = "@", Type = "MX", Address = "mx1.example.net", MXPref = 5}) setZone("MX", mailZone) namecheap.changeDnsRecords("example.com", {add = {{Name = "_dmarc", Type = "TXT", Address = "v=DMARC1; p=none"}}}) _, fields = form(requests[2].body) assert(fields.EmailType == "MX" and fields.RecordType4 == "MX" and fields.MXPref4 == "5") -- An EmailType that would drop remaining MX records is refused. setZone("MX", mailZone) rejects(function() namecheap.changeDnsRecords("example.com", {EmailType = "NONE", add = {{Name = "x", Type = "A", Address = "192.0.2.30"}}}) end, "needs EmailType MX", 1) -- Domains on other nameservers are refused. setZone("NONE", webZone(), {ourDNS = false}) rejects(function() namecheap.changeDnsRecords("example.com", {add = {{Name = "x", Type = "A", Address = "192.0.2.30"}}}) end, "uses custom nameservers", 1) -- Namecheap's IsSuccess="false" is an error, and nothing is re-read. setZone("NONE", webZone(), {reject = true}) local ok, err = pcall(function() namecheap.changeDnsRecords("example.com", {add = {{Name = "x", Type = "A", Address = "192.0.2.30"}}}) end) assert(not ok and string.find(tostring(err), "did not apply the change", 1, true) and #requests == 2) -- A read-back that differs from what was written is reported, not hidden. setZone("NONE", webZone(), {tamper = true}) result = namecheap.changeDnsRecords("example.com", {add = {{Name = "x", Type = "A", Address = "192.0.2.30"}}}) assert(result.Changed == true and result.Verified == false and string.find(result.VerifyError, "differ", 1, true)) assert(#result.Hosts == 5, "Hosts reflects the read-back records") -- Invalid changes never reach the network. local function invalid(change, text) setZone("NONE", webZone()) rejects(function() namecheap.changeDnsRecords("example.com", change) end, text, 0) end invalid({}, "add or remove at least one record") invalid({add = {{Name = "@", Type = "SRV", Address = "x"}}}, "add[1].Type must be one of") invalid({add = {{Name = "@", Type = "MX", Address = "mx.example.net"}}}, "add[1].MXPref must be an integer") invalid({add = {{Name = "@", Type = "A", Address = "192.0.2.1", MXPref = 10}}}, "MXPref is only for MX records") invalid({add = {{Name = "go", Type = "URL301", Address = "example.org"}}}, "must start with a protocol") invalid({add = {{Name = "@", Type = "A", Address = "192.0.2.1", TTL = 30}}}, "TTL must be an integer from 60 to 60000") invalid({add = {{Name = "@", Type = "A", Adress = "192.0.2.1"}}}, "unknown field Adress") invalid({remove = {{}}}, "remove[1] must name at least one") invalid({EmailType = "PROTON", add = {{Name = "@", Type = "A", Address = "192.0.2.1"}}}, "EmailType must be") invalid({replace = {}}, "unknown field replace") end, }dns_reads.luau
-- DNS, nameserver, domain detail and availability reads parse Namecheap's XML, including escaped values. local function xml(body: string): string return '<?xml version="1.0" encoding="utf-8"?><ApiResponse Status="OK" xmlns="http://api.namecheap.com/xml.response"><Errors />' .. '<!-- served by PHX01 --><CommandResponse Type="x">' .. body .. '</CommandResponse></ApiResponse>' end local RESPONSES = { ["namecheap.domains.dns.getHosts"] = xml('<DomainDNSGetHostsResult Domain="example.co.uk" EmailType="MX" IsUsingOurDNS="true">' .. '<host HostId="11" Name="@" Type="A" Address="192.0.2.10" MXPref="10" TTL="1799" AssociatedAppTitle="" FriendlyName="" IsActive="true" IsDDNSEnabled="false" />' .. '<host HostId="12" Name="@" Type="TXT" Address="v=spf1 include:_spf.example.net > ~all & "q" é☺" MXPref="10" TTL="300" IsActive="true" />' .. '<host HostId="13" Name="@" Type="MX" Address="mx1.example.net." MXPref="5" TTL="1799" IsActive="true"></host>' .. "<host HostId='14' Name='_dmarc' Type='TXT' Address='v=DMARC1; p=none; rua=mailto:[email protected]' MXPref='10' TTL='1799' IsActive='true'/>" .. '</DomainDNSGetHostsResult>'), ["namecheap.domains.dns.getList"] = xml('<DomainDNSGetListResult Domain="example.co.uk" IsUsingOurDNS="false" IsPremiumDNS="false" IsUsingFreeDNS="false">' .. '<Nameserver>ns1.example.net</Nameserver><Nameserver>ns2.example.net</Nameserver></DomainDNSGetListResult>'), ["namecheap.domains.getInfo"] = xml('<DomainGetInfoResult Status="Ok" ID="101" DomainName="example.co.uk" OwnerName="ada" IsOwner="true" IsPremium="false">' .. '<DomainDetails><CreatedDate>03/01/2020</CreatedDate><ExpiredDate>03/01/2027</ExpiredDate><NumYears>0</NumYears></DomainDetails>' .. '<Whoisguard Enabled="True"><ID>53536</ID><ExpiredDate>03/01/2027</ExpiredDate></Whoisguard>' .. '<DnsDetails ProviderType="FREE" IsUsingOurDNS="true" HostCount="4" EmailType="MX" DynamicDNSStatus="false" IsFailover="false">' .. '<Nameserver>dns1.registrar-servers.com</Nameserver></DnsDetails>' .. '<Modificationrights All="true"><Rights Type="dns">OK</Rights></Modificationrights></DomainGetInfoResult>'), ["namecheap.domains.check"] = xml('<DomainCheckResult Domain="example.com" Available="false" ErrorNo="0" Description="" IsPremiumName="false" PremiumRegistrationPrice="0" PremiumRenewalPrice="0" PremiumRestorePrice="0" PremiumTransferPrice="0" IcannFee="0" EapFee="0.0" />' .. '<DomainCheckResult Domain="example-new.io" Available="true" ErrorNo="0" Description="" IsPremiumName="true" PremiumRegistrationPrice="13000.0000" PremiumRenewalPrice="40.84" PremiumRestorePrice="65.00" PremiumTransferPrice="40.84" IcannFee="0.18" EapFee="0.0" />'), } return { scenario = {publisher = {}, auth_method = "token", auth_config = {token = "fixture-key"}, config = {api_host = "api.namecheap.com", username = "ada", client_ip = "203.0.113.7"}}, configure = function() requests = {} http = {request = function(req) table.insert(requests, req) local command = string.match(req.url, "[?&]Command=([^&]+)") local body = RESPONSES[command] assert(body, "unexpected command " .. tostring(command)) return fixture.operation({statusCode = 200, headers = {}, body = fixture.reader(body)}) end} end, run = function(namecheap) assert(namecheap.changeDnsRecords == nil, "access defaults to Read") local zone = namecheap.getDnsRecords("Example.CO.UK.") assert(string.find(requests[1].url, "&Command=namecheap.domains.dns.getHosts&SLD=example&TLD=co.uk", 1, true), requests[1].url) assert(zone.Domain == "example.co.uk" and zone.EmailType == "MX" and zone.IsUsingOurDNS == true) assert(#zone.Hosts == 4) local a, txt, mx, dmarc = zone.Hosts[1], zone.Hosts[2], zone.Hosts[3], zone.Hosts[4] assert(a.HostId == 11 and a.Name == "@" and a.Type == "A" and a.Address == "192.0.2.10" and a.TTL == 1799 and a.IsActive == true) assert(txt.Address == 'v=spf1 include:_spf.example.net > ~all & "q" ' .. utf8.char(233) .. utf8.char(0x263A), txt.Address) assert(mx.MXPref == 5 and mx.Address == "mx1.example.net.") assert(dmarc.Name == "_dmarc" and dmarc.Address == "v=DMARC1; p=none; rua=mailto:[email protected]", "single-quoted attributes") local ns = namecheap.getNameservers("example.co.uk") assert(ns.IsUsingOurDNS == false and #ns.Nameservers == 2 and ns.Nameservers[2] == "ns2.example.net") local info = namecheap.getDomain("example.co.uk") assert(string.find(requests[3].url, "&Command=namecheap.domains.getInfo&DomainName=example.co.uk", 1, true)) assert(info.ID == 101 and info.IsOwner == true and info.DomainName == "example.co.uk") assert(info.DomainDetails.ExpiredDate == "03/01/2027" and info.DomainDetails.NumYears == 0) assert(info.Whoisguard.Enabled == true and info.Whoisguard.ID == 53536) assert(info.DnsDetails.EmailType == "MX" and info.DnsDetails.HostCount == 4) assert(#info.DnsDetails.Nameserver == 1 and info.DnsDetails.Nameserver[1] == "dns1.registrar-servers.com", "a single nameserver stays an array") assert(#info.Modificationrights.Rights == 1 and info.Modificationrights.Rights[1].Type == "dns" and info.Modificationrights.Rights[1].Value == "OK") local check = namecheap.checkAvailability({"example.com", "EXAMPLE-NEW.io"}) assert(string.find(requests[4].url, "&DomainList=example.com%2Cexample-new.io", 1, true), requests[4].url) assert(#check.Results == 2) assert(check.Results[1].Available == false and check.Results[2].Available == true) assert(check.Results[2].IsPremiumName == true and check.Results[2].PremiumRegistrationPrice == 13000 and check.Results[2].IcannFee == 0.18) assert(#requests == 4) end, }errors.luau
-- Namecheap reports most failures inside an HTTP 200 XML envelope; each maps to a clear, classified error. local function envelope(status: string, inner: string): string return '<?xml version="1.0" encoding="utf-8"?><ApiResponse Status="' .. status .. '" xmlns="http://api.namecheap.com/xml.response">' .. inner .. '</ApiResponse>' end return { scenario = {publisher = {}, auth_method = "token", auth_config = {token = "fixture-key"}, config = {api_host = "api.namecheap.com", username = "ada", client_ip = "203.0.113.7"}}, access = {data = "read"}, configure = function() calls = 0 status = 200 payload = "" http = {request = function(req) calls += 1 return fixture.operation({statusCode = status, headers = {}, body = fixture.reader(payload)}) end} end, run = function(namecheap) local function upstream(code, body, expected) status, payload = code, body local before = calls local ok, err = pcall(function() namecheap.getDnsRecords("example.com") end) for _, text in expected do assert(not ok and string.find(tostring(err), text, 1, true), "missing " .. text .. " in " .. tostring(err)) end assert(calls == before + 1, "a failed call was retried") end upstream(200, envelope("ERROR", '<Errors><Error Number="1011150">Invalid request IP: 198.51.100.4</Error></Errors><CommandResponse />'), {"error 1011150", "Invalid request IP", "203.0.113.7 is whitelisted", '"retryable":false', '"layer":"upstream"'}) upstream(200, envelope("ERROR", '<Errors><Error Number="1011102">API Key is invalid or API access has not been enabled</Error></Errors>'), {"error 1011102", "API access has not been enabled", '"retryable":false'}) upstream(200, envelope("ERROR", '<Errors><Error Number="2019166">Domain not found</Error><Error Number="5050900">Unhandled exceptions</Error></Errors>'), {"error 2019166: Domain not found; error 5050900", '"retryable":true'}) upstream(405, "Too many requests", {"HTTP 405", "rate limit", '"retryable":true'}) upstream(503, "<html>Service Unavailable</html>", {"HTTP 503", '"retryable":true'}) upstream(200, "<html><body>Maintenance</body>", {"not its XML API format"}) upstream(200, '{"status":"ok"}', {"not its XML API format"}) upstream(200, envelope("ERROR", "<Errors />"), {"status ERROR without an error"}) upstream(200, envelope("OK", "<Errors />"), {"no CommandResponse"}) upstream(200, envelope("OK", '<Errors /><CommandResponse Type="x" />'), {"no DomainDNSGetHostsResult"}) end, }invalid_ip.luau
-- Namecheap requires an IPv4 ClientIp; an IPv6 or malformed setting fails clearly before any request. return { scenario = {publisher = {}, auth_method = "token", auth_config = {token = "fixture-key"}, config = {api_host = "api.sandbox.namecheap.com", username = "ada", client_ip = "2001:db8::7"}}, configure = function() calls = 0 http = {request = function(req) calls += 1 return fixture.operation({statusCode = 200, headers = {}, body = fixture.reader("")}) end} end, run = function(namecheap) for _, call in { function() namecheap.listDomains() end, function() namecheap.getDnsRecords("example.com") end, } do local ok, err = pcall(call) assert(not ok and string.find(tostring(err), "must be an IPv4 address", 1, true), tostring(err)) end assert(calls == 0, "a bad IP setting reached the network") end, }read_only.luau
-- Read connections export the reads alone, send GET requests with exact parameters, and never write. local function xml(body: string): string return '<?xml version="1.0" encoding="utf-8"?>\r\n<ApiResponse Status="OK" xmlns="http://api.namecheap.com/xml.response">' .. '<Errors /><Warnings /><RequestedCommand>namecheap.domains.getList</RequestedCommand>' .. '<CommandResponse Type="namecheap.domains.getList">' .. body .. '</CommandResponse>' .. '<Server>PHX01</Server><GMTTimeDifference>--5:00</GMTTimeDifference><ExecutionTime>0.04</ExecutionTime></ApiResponse>' end local PAGE = xml('<DomainGetListResult>' .. '<Domain ID="101" Name="example.com" User="ada" Created="03/01/2020" Expires="03/01/2027" IsExpired="false" IsLocked="true" AutoRenew="true" WhoisGuard="ENABLED" IsPremium="false" IsOurDNS="true"/>' .. '<Domain ID="102" Name="0001.io" User="ada" Created="05/02/2021" Expires="05/02/2026" IsExpired="true" IsLocked="false" AutoRenew="false" WhoisGuard="NOTPRESENT" IsPremium="false" IsOurDNS="false" />' .. '</DomainGetListResult><Paging><TotalItems>42</TotalItems><CurrentPage>2</CurrentPage><PageSize>20</PageSize></Paging>') local EMPTY = xml('<DomainGetListResult /><Paging><TotalItems>0</TotalItems><CurrentPage>1</CurrentPage><PageSize>20</PageSize></Paging>') local BASE = "https://api.namecheap.com/xml.response?ApiUser=ada&UserName=ada&ClientIp=203.0.113.7" return { scenario = {publisher = {}, auth_method = "token", auth_config = {token = "fixture-key"}, config = {api_host = "api.namecheap.com", username = "ada", client_ip = "203.0.113.7"}}, access = {data = "read"}, configure = function() requests = {} nextBody = PAGE http = {request = function(req) table.insert(requests, req) return fixture.operation({statusCode = 200, headers = {}, body = fixture.reader(nextBody)}) end} end, run = function(namecheap) assert(namecheap.changeDnsRecords == nil, "Read access must not export changeDnsRecords") for _, name in {"listDomains", "getDomain", "checkAvailability", "getNameservers", "getDnsRecords"} do assert(type(namecheap[name]) == "function", "missing read " .. name) end local page = namecheap.listDomains({ListType = "EXPIRING", SearchTerm = "ex ample&co", Page = 2, PageSize = 20, SortBy = "EXPIREDATE"}) local req = requests[1] assert(req.method == "GET" and req.body == nil and req.headers == nil) assert(req.url == BASE .. "&Command=namecheap.domains.getList&ListType=EXPIRING&Page=2&PageSize=20&SearchTerm=ex%20ample%26co&SortBy=EXPIREDATE", req.url) assert(not string.find(req.url, "ApiKey", 1, true), "the API key is injected by Houston, never by the connector") assert(page.TotalItems == 42 and page.CurrentPage == 2 and page.PageSize == 20) assert(#page.Domains == 2) local first, second = page.Domains[1], page.Domains[2] assert(first.ID == 101 and first.Name == "example.com" and first.IsLocked == true and first.AutoRenew == true) assert(first.Expires == "03/01/2027" and first.WhoisGuard == "ENABLED" and first.IsOurDNS == true) assert(second.Name == "0001.io" and second.IsExpired == true and second.IsOurDNS == false, "names stay text") nextBody = EMPTY local empty = namecheap.listDomains() assert(requests[2].url == BASE .. "&Command=namecheap.domains.getList") assert(#empty.Domains == 0 and json.encode(empty.Domains) == "[]" and empty.TotalItems == 0) local function rejects(fn, text) local before = #requests local ok, err = pcall(fn) assert(not ok and string.find(tostring(err), text, 1, true), "expected " .. text .. ", got " .. tostring(err)) assert(#requests == before, "invalid input reached the network") end rejects(function() namecheap.listDomains({ListType = "SOON"}) end, "ListType must be") rejects(function() namecheap.listDomains({PageSize = 5}) end, "PageSize must be an integer from 10 to 100") rejects(function() namecheap.listDomains({Page = 1.5}) end, "Page must be an integer") rejects(function() namecheap.listDomains({Sort = "NAME"}) end, "unknown field Sort") rejects(function() namecheap.getDnsRecords("https://example.com/") end, "registered domain name") rejects(function() namecheap.getDnsRecords("") end, "domain must be a nonempty string") rejects(function() namecheap.checkAvailability({}) end, "1 to 50 domain names") local many = {} for i = 1, 51 do many[i] = "d" .. i .. ".com" end rejects(function() namecheap.checkAvailability(many) end, "1 to 50 domain names") for _, req2 in requests do assert(req2.method == "GET", "Read connections only send GET") end end, }