Authentication and signing
Sign every request with HMAC-SHA256: the headers, the string to sign and working code.
Every Energy Rental API request carries your API key, a timestamp and a signature made with your secret. The base URL, formats and rate limits are on Conventions and limits.
Headers
Send all three headers on every request, including GET requests.
X-API-Key
Your API key
X-Timestamp
Current Unix time in seconds. Must be within 30 seconds of our clock
X-Signature
HMAC-SHA256 signature of the request, as lowercase hex
A request with a body also sends Content-Type: application/json.
A timestamp outside the 30-second tolerance fails with 401 UNAUTHORIZED. Keep your servers on network time (NTP) and read the clock when you sign each request, not once at start-up.
String to sign
The signature covers four elements, joined by single newline characters.
STRING_TO_SIGN = METHOD + "\n" + PATH + "\n" + TIMESTAMP + "\n" + BODY
SIGNATURE = lowercase_hex(HMAC_SHA256(key = API_SECRET, message = STRING_TO_SIGN))METHOD
The HTTP method
Upper case, for example GET or POST
PATH
The request path with its query string, relative to the base URL
Starts with /v1/. Include the ? and the query string exactly as you send it, in the same order. Leave out the scheme and the host
TIMESTAMP
The value of the X-Timestamp header
Unix time in whole seconds, digits only
BODY
The request body
The exact bytes you send. Empty for GET
\n
The separator
One line feed character (byte 0x0A). No carriage return
The string ends with BODY and nothing else. For a GET request BODY is empty, so the string ends with the newline that follows TIMESTAMP. Two examples, with \n standing for the newline. Sample IDs, addresses and hashes are illustrative.
Generate the signature
Serialise the JSON body once. Use an empty string for a
GET.Read the clock in whole seconds. This is
TIMESTAMPand theX-Timestampvalue.Join
METHOD,PATH,TIMESTAMPandBODYwith\n.Compute the HMAC-SHA256 of that string with your secret and encode it as lowercase hex. This is the
X-Signaturevalue.Send the three headers and the body from step 1, unchanged.
Sign what you send. A client that re-serialises the body, with different spacing or key order, sends different bytes and the signature no longer matches. Sign each retry again with a new timestamp.
Signing helpers
The helpers sign a request and send it. The Node.js and Python helpers return the HTTP status and the parsed response; signed_curl in tron_energy.sh prints the response body. They read BASE_URL, API_KEY and API_SECRET from the environment, and the path you pass includes the query string. Other pages in this section call them.
Export BASE_URL, API_KEY and API_SECRET, then compute the signature inline with openssl. The variable is REQUEST_PATH because PATH is the shell's own variable.
A POST with a body:
A GET has an empty body and no Content-Type. The query string is part of the signed path:
To sign each call with one command, save this function as tron_energy.sh and load it into your shell with source tron_energy.sh. The Quickstart's cURL steps call it.
The -hmac option puts the secret on the command line, where other users of the machine can see it in the process list. Use the recipe for testing from your own machine and the Node.js or Python helper in production.
Requires Node.js 18 or later. The helper uses the built-in fetch and crypto modules, so there is nothing to install. Save it as tron_energy.mjs.
Call it from any .mjs file:
Requires Python 3.9 or later and requests (pip install requests). Save the helper as tron_energy.py.
Call it from any Python file in the same folder:
Test vector
A test vector is a fixed secret, timestamp and request, with the signature they must produce. Until the official one is published, check your helper against the openssl recipe.
The three commands below sign the same dummy request, so they must print the same 64-character string. A match shows that your code builds the string described above.
Troubleshooting 401 errors
Every authentication failure returns 401 UNAUTHORIZED with the same code, so check the causes below in order.
Clock drift
X-Timestamp is more than 30 seconds from our clock
Synchronise the server clock with NTP and read the clock for each request
Timestamp in milliseconds
Date.now() and System.currentTimeMillis() return milliseconds
Send whole seconds, for example Math.floor(Date.now() / 1000)
Body differs from the signed string
The body was re-serialised after signing, with different spacing or key order, or gained a trailing newline
Serialise once, then sign and send that same string
Query string left out of the path
You signed /v1/rentals and sent /v1/rentals?status=reclaimed
Sign the path with its query string, in the order you send it
Full URL signed
You signed https://host/v1/rentals instead of the path
Sign the path only, starting with /v1/
Body signed on a GET
The string holds a body for a request that sends none
Use an empty BODY for GET
Wrong separator
\r\n instead of \n, or a GET string that lost the newline after the timestamp
Join with \n only. A GET string ends with the newline after TIMESTAMP
Lower-case method
You signed post and sent POST
Use the upper-case method in the string and in the request
Signature encoding
Base64, upper-case hex or raw bytes
Send lowercase hex
Wrong key or secret
A misspelt header name, a space or newline copied with the secret, or a key and secret that were not issued together
Check the header names, trim the values and use the pair we issued together
If a request still fails, contact our team with the request_id from the error body, the method and path you signed and the timestamp you sent. Never send your secret. Getting access lists how to reach us.
Handling credentials
Store the API key and secret in a secrets manager or in environment variables on your servers.
Call the API from your backend only, not from browsers or mobile apps. Any client that signs requests holds the secret.
The secret signs every request and is never sent with it. Keep it out of source control, logs and error reports.
Credentials can be rotated on request. Getting access explains how credentials are issued.
Next steps
Last updated