Authentication
Every request except GET /api/v1/time carries your key id and an HMAC-SHA256 signature. There are no bearer tokens and no sessions: each request proves on its own that it came from the holder of the secret, was made in the last few seconds and has not been seen before.
Credentials#
The desk issues credentials in pairs. The key id is public and travels with every request. The secret never leaves your servers: it is only the key for the HMAC.
| Item | Format | Length | How it is used |
|---|---|---|---|
| Live key id | mq_live_pk_ + 22 characters | 33 | Sent in X-MQ-API-KEY on every request |
| Live secret | mq_live_sk_ + 49 characters | 60 | Never sent. Used only as the HMAC key |
| Test key id | mq_test_pk_ + 22 characters | 33 | Sent in X-MQ-API-KEY on every request |
| Test secret | mq_test_sk_ + 49 characters | 60 | Never sent. Used only as the HMAC key |
- The last six characters of every key id and secret are a checksum, so a mistyped key is rejected straight away.
- The secret is shown once, when the key is issued or rotated. Nobody at MQuant can read it back to you. If you lose it, ask the desk to rotate the key.
- Each key has scopes:
signals:writeto send, test and cancel signals, andsignals:readto read them back.GET /meworks with either scope. - Each key has an expiry, 90 days by default and at most 365 days.
GET /api/v1/meshowsexpires_at; after it, requests fail with401 key_expired. - A trader can hold at most two active or suspended keys per mode. That leaves room to run a new key beside the old one while you switch over.
Request headers#
| Header | Format | Example |
|---|---|---|
X-MQ-API-KEY | Your key id | mq_live_pk_… |
X-MQ-TIMESTAMP | Unix epoch time in milliseconds, exactly 13 digits | 1796184000000 |
X-MQ-NONCE | 16 to 64 characters from A-Z a-z 0-9 _ -, new for every request | 6f1c2a1e-8a4b-4c1f-9a3e-2b7d5c9e0f11 |
X-MQ-SIGNATURE | 64 hex characters: HMAC-SHA256 of the canonical string | 4f3288cd…eb0e |
A POST with a body also needs Content-Type: application/json, optionally with ; charset=utf-8. Any other media type or charset returns 415 unsupported_media_type, and so does a Content-Encoding other than identity: do not compress request bodies. A cancel sent with no body needs no Content-Type.
Bodies are capped at 16,384 bytes (65,536 bytes for POST /signals/batch); larger bodies return 413 payload_too_large. A GET must not have a body (400 body_not_allowed).
The canonical string#
The signature is the HMAC-SHA256 of six values joined with a single line feed (\n, byte 0x0A). There is no trailing line feed.
canonical = METHOD + "\n" + PATH_WITH_QUERY + "\n" + TIMESTAMP + "\n" + NONCE + "\n" + KEY_ID + "\n" + BODY_SHA256_HEX
signature = lowercase_hex(HMAC_SHA256(key = secret_ascii_bytes, message = canonical_ascii_bytes))- METHOD: upper case,
GETorPOST. - PATH_WITH_QUERY: the request target exactly as you send it, starting at
/api/v1, plus?and the query string when there is one. MQuant signs the raw bytes it receives and never decodes or reorders the query, so sign the string you put on the wire, in the same order and with the same percent-encoding. - TIMESTAMP, NONCE and KEY_ID: the header values, exactly as sent.
- BODY_SHA256_HEX: the lowercase hex SHA-256 of the raw body bytes. For an empty body it is always
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855. - The HMAC key is the whole 60-character secret, prefix included, as ASCII bytes.
- Send the result as 64 hex characters. Lower case is conventional; upper case is accepted.
Worked example#
These inputs are for the documentation only and are not real credentials. Run your own signing code on them: if it prints the same body hash and signature, your signing is right. The body uses a fixed expiry date so that the signature never changes; do not send it as a real signal.
- Method
POST- Path
/api/v1/signals- Timestamp
1796184000000- Nonce
6f1c2a1e-8a4b-4c1f-9a3e-2b7d5c9e0f11- Key id
mq_test_pk_Docs0Example0Key01tQIQ- Secret
mq_test_sk_DocsOnlyExampleSecretNeverUseThisValue123453XfwfW
{"client_signal_id":"nifty-0001","exchange":"NFO","symbol":"NIFTY","instrument_type":"CE","expiry":"2026-12-29","strike":"24500","side":"BUY","lots":2,"order_type":"LIMIT","price":"112.50","product":"NRML","validity":"DAY","tag":"momo1"}-
Hash the body
Take the exact bytes above (UTF-8, no added whitespace, no trailing newline) and compute SHA-256 as lowercase hex.
- Body SHA-256
aeca1167ee27e92c043810b6c5dd20eda70395d6adc31b530df49d4f2bff58d6
-
Build the canonical string
Join the six values with
\n. Here every line ends with a line feed except the last.MethodPOSTPath with query/api/v1/signalsTimestamp1796184000000Nonce6f1c2a1e-8a4b-4c1f-9a3e-2b7d5c9e0f11Key idmq_test_pk_Docs0Example0Key01tQIQBody SHA-256aeca1167ee27e92c043810b6c5dd20eda70395d6adc31b530df49d4f2bff58d6Signature4f3288cd2ddecc0d5d2e692ae6081b02548a02eebc09be41b1d58d4071c2eb0e -
Sign it
HMAC-SHA256 with the secret as the key and the canonical string as the message, both as ASCII bytes, written as lowercase hex.
- Signature
4f3288cd2ddecc0d5d2e692ae6081b02548a02eebc09be41b1d58d4071c2eb0e
-
Send the four headers
On the wirePOST /api/v1/signals HTTP/1.1 Host: app.mquant.in Content-Type: application/json X-MQ-API-KEY: mq_test_pk_Docs0Example0Key01tQIQ X-MQ-TIMESTAMP: 1796184000000 X-MQ-NONCE: 6f1c2a1e-8a4b-4c1f-9a3e-2b7d5c9e0f11 X-MQ-SIGNATURE: 4f3288cd2ddecc0d5d2e692ae6081b02548a02eebc09be41b1d58d4071c2eb0e {"client_signal_id":"nifty-0001","exchange":"NFO","symbol":"NIFTY","instrument_type":"CE","expiry":"2026-12-29","strike":"24500","side":"BUY","lots":2,"order_type":"LIMIT","price":"112.50","product":"NRML","validity":"DAY","tag":"momo1"}
A second check, for a GET with a query string and an empty body, using the same timestamp, nonce and key:
To check your setup in one go, this short script must print the body hash and then the signature shown above:
import hashlib
import hmac
secret = "mq_test_sk_DocsOnlyExampleSecretNeverUseThisValue123453XfwfW"
body = b'{"client_signal_id":"nifty-0001","exchange":"NFO","symbol":"NIFTY","instrument_type":"CE","expiry":"2026-12-29","strike":"24500","side":"BUY","lots":2,"order_type":"LIMIT","price":"112.50","product":"NRML","validity":"DAY","tag":"momo1"}'
body_hash = hashlib.sha256(body).hexdigest()
canonical = "\n".join(["POST", "/api/v1/signals", "1796184000000",
"6f1c2a1e-8a4b-4c1f-9a3e-2b7d5c9e0f11",
"mq_test_pk_Docs0Example0Key01tQIQ", body_hash])
print(body_hash)
print(hmac.new(secret.encode("ascii"), canonical.encode("ascii"), hashlib.sha256).hexdigest())Common mistakes#
- Re-serialising JSON after signing. Build the body string once, hash it and send those same bytes. Many HTTP libraries re-encode a dict you hand them; pass the string or bytes instead.
- Signing a different query string from the one you send. If your library sorts or re-encodes parameters, build the query yourself and pass the full URL.
- Timestamps in seconds. MQuant wants 13-digit milliseconds. A 10-digit value fails with
auth_headers_invalid. - Reusing a nonce on a retry. Every attempt needs a fresh timestamp, nonce and signature. Keep the same
client_signal_idinstead; that is what makes a retry safe. - Signing the full URL. Sign the path from
/api/v1, nothttps://app.mquant.in/api/v1/…. - A trailing slash.
/api/v1/signals/is a different path and returns404 not_found.
Every signature failure gets the same 401 auth_failed response, whether the key id is unknown, the secret is wrong or the body was changed on the way. MQuant does not say which part was wrong. Debug against the worked example.
Clock and timestamps#
A request is accepted only when its timestamp is no more than 5,000 ms behind and no more than 1,000 ms ahead of the server clock. Outside that window you get 401 timestamp_out_of_window, and the message includes the server time in milliseconds.
- Run NTP (chrony or systemd-timesyncd) on every machine that signs requests.
GET /api/v1/timeneeds no signature and returnsserver_time_ms, plus the currentrecv_window_msandfuture_skew_ms. Read these values rather than hard-coding them.- If you cannot fix the clock, measure the offset: note your time before and after calling
/time, take the midpoint, and addserver_time_ms − midpointto every timestamp. The Python and Node clients do this insync_clock()/syncClock(). - Sign immediately before you send. A request that sits in a queue for seconds can expire before it arrives.
Nonces#
- 16 to 64 characters from
A–Z a–z 0–9 _ -. A fresh UUIDv4 or 16 random bytes as hex is ideal. - Each nonce can be used once per key. MQuant remembers it for longer than the timestamp window, so a captured request cannot be replayed. A repeat returns
401 nonce_reused. - The nonce is recorded only after the signature checks out, so nobody else can use up your nonces.
- Never derive the nonce from the signal or the clock alone. Two requests in the same millisecond must still have different nonces.
What the server checks, in order#
Checks run in this order, and the first failure decides the response. Knowing the order helps you read an error.
| Step | Check | Failure |
|---|---|---|
| 1 | Per-IP request limit and any lockout or block on your address | 429 rate_limited, auth_locked, ip_temporarily_blocked |
| 2 | API switched on | 503 api_disabled |
| 3 | Body size, Content-Type, Content-Encoding, no body on GET | 413 payload_too_large, 415 unsupported_media_type, 400 body_not_allowed |
| 4 | The four headers are present once each and well formed | 401 auth_headers_invalid |
| 5 | Timestamp inside the window | 401 timestamp_out_of_window |
| 6 | Key id and signature | 401 auth_failed |
| 7 | Nonce not seen before | 401 nonce_reused |
| 8 | Key and trader state | 401 key_expired, key_suspended, trader_inactive |
| 9 | Caller IP on the allowlist | 403 ip_not_allowed |
| 10 | Key has the scope the endpoint needs | 403 scope_denied |
| 11 | Key not temporarily blocked | 429 key_temporarily_blocked |
| 12 | Signal intake not paused (sending signals only) | 503 intake_paused |
| 13 | Key, trader and desk rate limits | 429 rate_limited |
| 14 | Body is strict JSON and the signal is valid, then risk caps | 400, 409 or 422, see Errors |
IP allowlist#
Live keys accept requests only from the addresses on their allowlist. Anything else gets 403 ip_not_allowed, with your address in the message.
- A live key needs 1 to 5 entries. Each is a public IPv4 or IPv6 address, or a CIDR range no wider than
/24(IPv4) or/56(IPv6). - A test key may have an empty allowlist, which allows any address. If it has entries, they are enforced.
- The desk changes allowlists for you. On a live key the list can change once every 7 days, in line with the exchange static-IP rule, so plan moves to new servers ahead.
- The address that counts is the one MQuant sees: your NAT gateway or egress IP, not the private address of your server.
GET /api/v1/mereturns it ascaller_ip.
Key rotation and expiry#
Rotation gives you a new secret for the same key id. Ask the desk for it on a schedule, when someone with access leaves, or at once if you think the secret has leaked.
- The desk rotates the key and hands you the new secret, shown once. The key id, limits, allowlist and your
client_signal_idhistory do not change. - For a grace period set by the desk (24 hours by default, at most 72 hours) both the old and the new secret are accepted.
GET /api/v1/meshows the end of it inrotation_grace_until. - Deploy the new secret to every machine and confirm with
GET /api/v1/me. - When the grace period ends, the old secret stops working. A rotation with no grace period switches at once.
Other key states you may meet: a suspended key returns 401 key_suspended until the desk resumes it. A revoked key is gone for good: its secret is erased, so its requests fail with 401 auth_failed. An expired key returns 401 key_expired; ask the desk for a new key before expires_at.
Security checklist#
- Keep the secret on the server. Load it from a secrets manager or an environment variable. Never put it in source control, a browser, a mobile app, a spreadsheet, a chat message or a log line.
- Do not log credentials. Log the
X-Request-Idand yourclient_signal_id, not the signature or the secret. A signature is single-use but still tells an attacker about your traffic. - One key per system. Give each trading box its own key, so you can rotate or revoke one without stopping the others.
- Use HTTPS and verify certificates. Never turn off certificate verification, even when testing.
- Lock down the shell example.
openssl -hmactakes the secret as a command-line argument, which other users on the same machine can see in the process list. Use it only on a single-user machine, or use the Python or Node client. - Least privilege. A monitoring job that only reads signals should have a key with
signals:readonly. - If a secret leaks, tell your MQuant desk contact at once. A revoke takes effect on the very next request.