Token URL
MarsX gets an access token from your token URL before it reads your other endpoints. You build one endpoint that follows OAuth 2.0 client credentials (RFC 6749 section 4.4), and you issue MarsX one read-only client, the MarsX read client, for each connection and environment.
The token request
Section titled “The token request”MarsX signs in with HTTP Basic client authentication. The scope is leads:read. It is read-only.
POST <your token URL>Content-Type: application/x-www-form-urlencodedAuthorization: Basic <base64(client_id:client_secret)>
grant_type=client_credentials&scope=leads:read{ "access_token": "example-access-token", "token_type": "Bearer", "expires_in": 3600, "scope": "leads:read"}The response follows the PartnerTokenResponse schema in the contract. access_token, token_type (Bearer) and expires_in are required. scope is optional. MarsX ignores any other member.
| Field | Rule |
|---|---|
expires_in |
Between 300 and 86,400 seconds. 3600 is a good value |
| Renewal | MarsX asks for a new token 30 seconds before it expires |
| Scope | Read-only. You name it |
What MarsX does with the token
Section titled “What MarsX does with the token”Every read call carries Authorization: Bearer <access_token>.
| Case | Your endpoint answers | What MarsX does |
|---|---|---|
| Wrong or expired token | 401 |
Asks for a new token once, then retries the read |
| Valid token without the scope | 403 |
Does not retry by itself. It treats this as a contract failure |
Errors from the token URL
Section titled “Errors from the token URL”Errors follow RFC 6749 section 5.2. Answer with JSON that has an error member.
| Case | Status | error |
|---|---|---|
| Unknown client or wrong secret | 401 |
invalid_client |
grant_type is not client_credentials |
400 |
unsupported_grant_type |
| The scope is not one you offer | 400 |
invalid_scope |
| Wrong content type or a missing parameter | 400 |
invalid_request |
Share the client secret safely
Section titled “Share the client secret safely”Your developer enters the client ID and secret in Concour: Developers, then Your endpoints, in the fields Client ID and Client secret (the client has the scope leads:read). The secret is saved and never shown again. Leave the field empty on a later save to keep the saved secret. Never paste a secret into email or WhatsApp.
A minimal token endpoint
Section titled “A minimal token endpoint”These examples show the shape of the answers. They keep tokens in memory and read the client from environment variables. Run them behind HTTPS, and keep tokens in a shared store if you run more than one server. Your read endpoints check an Authorization: Bearer header against the same store: a token that is unknown or past its expiry gets 401, and a valid token without the scope gets 403.
import { createServer } from "node:http";import { randomBytes, timingSafeEqual } from "node:crypto";
const CLIENT_ID = process.env.MARSX_CLIENT_ID;const CLIENT_SECRET = process.env.MARSX_CLIENT_SECRET;const SCOPE = "leads:read";const EXPIRES_IN = 3600;const tokens = new Map(); // access_token -> expiry, in milliseconds since the epoch
const same = (a, b) => { const x = Buffer.from(a); const y = Buffer.from(b); return x.length === y.length && timingSafeEqual(x, y);};
function basicCredentials(req) { const [scheme, encoded] = (req.headers.authorization ?? "").split(" "); if (scheme !== "Basic" || !encoded) return null; const text = Buffer.from(encoded, "base64").toString(); const i = text.indexOf(":"); return i < 0 ? null : [text.slice(0, i), text.slice(i + 1)];}
function reply(res, status, body, headers = {}) { res.writeHead(status, { "content-type": "application/json", "cache-control": "no-store", pragma: "no-cache", ...headers, }); res.end(JSON.stringify(body));}
createServer(async (req, res) => { const creds = basicCredentials(req); if (!creds || !same(creds[0], CLIENT_ID) || !same(creds[1], CLIENT_SECRET)) { return reply(res, 401, { error: "invalid_client" }, { "www-authenticate": "Basic" }); } let raw = ""; for await (const chunk of req) raw += chunk; const form = new URLSearchParams(raw); const isForm = (req.headers["content-type"] ?? "").startsWith("application/x-www-form-urlencoded"); if (req.method !== "POST" || !isForm || !form.get("grant_type")) { return reply(res, 400, { error: "invalid_request" }); } if (form.get("grant_type") !== "client_credentials") { return reply(res, 400, { error: "unsupported_grant_type" }); } if (form.has("scope") && form.get("scope") !== SCOPE) { return reply(res, 400, { error: "invalid_scope" }); } const accessToken = randomBytes(32).toString("base64url"); tokens.set(accessToken, Date.now() + EXPIRES_IN * 1000); reply(res, 200, { access_token: accessToken, token_type: "Bearer", expires_in: EXPIRES_IN, scope: SCOPE });}).listen(8080);import base64, hmac, json, os, secrets, timefrom http.server import BaseHTTPRequestHandler, HTTPServerfrom urllib.parse import parse_qs
CLIENT_ID = os.environ["MARSX_CLIENT_ID"]CLIENT_SECRET = os.environ["MARSX_CLIENT_SECRET"]SCOPE = "leads:read"EXPIRES_IN = 3600tokens = {} # access_token -> expiry, in seconds since the epoch
def same(a: str, b: str) -> bool: return hmac.compare_digest(a.encode(), b.encode())
class TokenHandler(BaseHTTPRequestHandler): def reply(self, status, body, headers=None): data = json.dumps(body).encode() self.send_response(status) self.send_header("Content-Type", "application/json") self.send_header("Cache-Control", "no-store") self.send_header("Pragma", "no-cache") for name, value in (headers or {}).items(): self.send_header(name, value) self.send_header("Content-Length", str(len(data))) self.end_headers() self.wfile.write(data)
def basic_credentials(self): scheme, _, encoded = self.headers.get("Authorization", "").partition(" ") if scheme != "Basic": return None try: client_id, colon, client_secret = base64.b64decode(encoded).decode().partition(":") except ValueError: return None return (client_id, client_secret) if colon else None
def do_POST(self): creds = self.basic_credentials() if not creds or not same(creds[0], CLIENT_ID) or not same(creds[1], CLIENT_SECRET): return self.reply(401, {"error": "invalid_client"}, {"WWW-Authenticate": "Basic"}) raw = self.rfile.read(int(self.headers.get("Content-Length", 0))).decode() form = parse_qs(raw) is_form = self.headers.get("Content-Type", "").startswith("application/x-www-form-urlencoded") if not is_form or "grant_type" not in form: return self.reply(400, {"error": "invalid_request"}) if form["grant_type"][0] != "client_credentials": return self.reply(400, {"error": "unsupported_grant_type"}) if "scope" in form and form["scope"][0] != SCOPE: return self.reply(400, {"error": "invalid_scope"}) access_token = secrets.token_urlsafe(32) tokens[access_token] = time.time() + EXPIRES_IN self.reply(200, {"access_token": access_token, "token_type": "Bearer", "expires_in": EXPIRES_IN, "scope": SCOPE})
HTTPServer(("", 8080), TokenHandler).serve_forever()
