API
Description
The Nxt API allows interaction with Nxt nodes using HTTP requests to port 7876. Most HTTP requests can use either the GET or POST methods, but some API calls accept only the POST method for security reasons. Responses are returned as JSON objects.
Each API call is documented below, with definitions given for HTTP request parameters and JSON response fields, followed by an example:
- The JSON response fields are each followed by one of S for string, A for array, O for object, N for number or B for boolean.
- In the examples, the Nxt node is represented as localhost and requests and responses are formatted for easy reading; line breaks and spaces are not actually used except in some parameter values. All requests are in URL format which implies the HTTP GET method. When GET is allowed, the URL can be entered into a browser URL field but proper URL encoding is usually required (e.g., spaces in a parameter value must be replaced by + or %20). Otherwise, the URL should be used as a guide to preparing an HTTP POST request using cURL, for example.
All API calls can be viewed and tested at http://localhost:7876/test while the local server node is running. For specific API calls, use http://localhost:7876/test?requestType=specificRequestType.
This document corresponds to Nxt Software Version 1.11.5.
General Notes
Genesis Block
Many API requests make reference to the genesis block. FYI, the genesis block's ID is 2680262203532249785. Sending messages, selling aliases, and leasing balances to the Genesis account are no longer allowed since the Monetary System block.
Flexible Account IDs
All API requests that require an account ID accept either an account number or the corresponding Reed-Solomon address.
Quantity Units NXT, NQT and QNT
The Nxt system has a currency NXT used to quantify value in the system. Like all currencies, NXT circulates in the system, moving from one user to another by payments and purchases. Also, a small fee is required for every transaction, including those in which no NXT is transfered, such as simple messages. This fee goes to the owner of the node that forges (generates) the new block containing the transaction that is accepted onto the blockchain.
One billion NXT were created in the Genesis Block, and no new NXT will ever be created. As of block 134,000 NXT became divisible to eight decimal places. Yet internally, the currency is still stored in integer form in units of NQT or NxtQuant, where 1 NXT = 108 NQT. All parameters and fields in the API involving a quantity of NXT are denominated in units of NQT, for example feeNQT. The only exception is the field effectiveBalanceNXT, used in forging calculations.
The Nxt system can be thought of as an asset owned by all who posses NXT. In this sense, NXT quantifies ownership of or stake in the system. Stakeholders are entitled to forge blocks and collect transaction fees in proportion to the amount of NXT they possess.
Other assets can be created within Nxt using Issue Asset. The issuer must specify the number of decimal places to use in quantifying the asset, and the amount of the asset to create in generic units of QNT or Quant, distinct from NQT. Quantities of assets are stored internally as integers in units of QNT, and assets are priced in NQT per QNT.
For example, the mgwBTC (multi-gateway Bitcoin) asset is divisible to eight decimal places, like the original bitcoin (BTC) it is a proxy for. Therefore a QNT of mgwBTC is equivalent to a Satoshi (10-8 BTC). To place an order to sell 25,000,000 QNT of the mgwBTC asset at a price of 20,000 NQT per QNT, use the API Call Place Order with requestType=placeAskOrder, quantityQNT=25000000 and priceNQT=20000. If the entire quantity is sold on the Asset Exchange, the seller will receive 500,000,000,000 NQT (quantityQNT * priceNQT) from the buyer(s). This trade corresponds to selling 0.25 mgwBTC for 5,000 NXT, which is how the trade appears in the NRS client. In this case, the price of 20,000 in NQT per QNT is also the price in NXT per mgwBTC, since both mgwBTC and NXT are divisible to eight decimal places.
Currencies in the Nxt Monetary System are a special kind of asset with properties and exchange methods suitable for currencies. With respect to quantities, exchange rates and decimal places, the Monetary System API calls use NXT, NQT and QNT in exactly the same way as assets.
Creating Unsigned Transactions
All API requests that create a new transaction will accept either a secretPhrase or a publicKey parameter:
- If secretPhrase is supplied, a transaction is created, signed at the server, and broadcast by the server as usual.
- If only a publicKey parameter is supplied as a 64-digit (32-byte) hex string, the transaction will be prepared by the server and returned in the JSON response as transactionJSON without a signature. This JSON object along with secretPhrase can then be supplied to Sign Transaction as unsignedTransactionJSON and the resulting signed transactionJSON can then be supplied to Broadcast Transaction. This sequence allows for offline signing of transactions so that secretPhrase never needs to be exposed.
- unsignedTransactionBytes may be used instead of unsigned transactionJSON when there is no encrypted message. Messages to be encrypted require the secretPhrase for encryption and so cannot be included in unsignedTransactionBytes.
Escrow Operations
All API requests that create a new transaction will accept an optional referencedTransactionFullHash parameter which creates a chained transaction, meaning that the new transaction cannot be confirmed unless the referenced transaction is also confirmed. This feature allows a simple way of transaction escrow:
- Alice prepares and signs a transaction A, but doesn't broadcast it by setting the broadcast parameter to false. She sends to Bob the unsignedTransactionBytes, the fullHash of the transaction, and the signatureHash. All of those are included in the JSON returned by the API request. (Warning: make sure not to send the signed transactionBytes, or the signature itself, as then Bob can just broadcast transaction A himself).
- Bob prepares, signs and broadcasts transaction B, setting the referencedTransactionFullHash parameter to the fullHash of A provided by Alice. He can verify that this hash indeed belongs to the transaction he expects from Alice, by using Calculate Full Hash, which takes unsignedTransactionBytes and signatureHash (both of which Alice has also sent to Bob). He can also use Parse Transaction to decode the unsigned bytes and inspect all transaction fields.
- Transaction B is accepted in the unconfirmed transaction pool, but as long as A is still missing, B will not be confirmed, i.e. will not be included in the blockchain. If A is never submitted, B will eventually expire -- so Bob should make sure to set a long enough deadline, such as the maximum of 32767 minutes.
- Once in the unconfirmed transactions pool, Bob has no way of recalling B back. So now Alice can safely submit her transaction A, by just broadcasting the signedTransactionBytes she got in the first step. Transaction A will get included in the blockchain first, and in the next block Bob's transaction B will also be included.
Note that while the above scheme is good enough for a simple escrow, the blockchain does not enforce that if A is included, B will also be included. It may happen due to forks and blockchain reorganization, that B never gets a chance to be included and expires unconfirmed, while A still remains in the blockchain. However, it is not practically possible for Bob to intentionally cause such chain of events and to prevent B from being confirmed.
Prunable Data
Prunable data can be removed from the blockchain without affecting the integrity of the blockchain. When a transaction containing prunable data is created, only the 32-byte sha256 hash of the prunable data is included in the transactionBytes, not the prunable data itself. The non-prunable signed transactionBytes are used to verify the signature and to generate the transaction's fullHash and ID; when the prunable part of the transaction is removed at a later time, none of these operations are affected.
Prunable data has a predetermined minimum lifetime of two weeks (24 hours on the Testnet) from the timestamp of the transaction. Transactions and blocks received from peer nodes are not accepted if prunable data is missing before this time has elapsed. After this time has elapsed, prunable data is no longer included with transactions and blocks transmitted to peer nodes, and is no longer included in the transaction JSON returned by general-purpose API calls such as Get Transaction; the only way to retrieve it, if still available, is through special-purpose API calls such as Get Prunable Message.
Expired prunable data remains stored in the blockchain until removed at the same time derived tables are trimmed, which occurs automatically every 1000 blocks by default. Use Trim Derived Tables to remove expired prunable data immediately.
Prunable data can be preserved on a node beyond the predetermined minimum lifetime by setting the nxt.maxPrunableLifetime property to a larger value than two weeks or to -1 to preserve it indefinitely. To force the node to include such preserved prunable data when transactions and blocks are transmitted to peer nodes, set the nxt.includeExpiredPrunables property to true, thus making it an archival node.
Currently, there are two varieties of prunable data in the Nxt system: prunable Arbitrary Messages and Tagged Data. Both varities have a maximum prunable data length of 42 kilobytes.
Properties Files
The behavior of some API calls is affected by property settings loaded from files in the nxt/conf directory during Nxt server intialization. This directory contains the nxt-default.properties and logging-default.properties files, both of which contain default property settings along with documentation. A few of the property settings can be obtained while the server is running through the Get Blockchain Status and Get State calls.
It is recommended not to modify default properties files because they can be overwritten during software updates. Instead, properties in the default files can be overridden by including them in optional nxt.properties and logging.properties files in the same directory. For example, a nxt.properties file can be created with the contents:
nxt.isTestnet=true
This causes the Nxt server to connect to the Testnet instead of the Mainnet.
Admin Password
Some API functions take an adminPassword parameter, which must match the nxt.adminPassword property unless the nxt.disableAdminPassword property is set to true or the API server only listens on the localhost interface (when the nxt.apiServerHost property is set to 127.0.0.1).
All Debug Operations require adminPassword since they require some kind of privilege. On some functions adminPassword is used so you can override maximum allowed value for lastIndex parameter, which is set to 100 by default by the nxt.maxAPIRecords property. Giving you the option to retrieve more than objects per request.
Roaming and Light Client Modes
The remote node to use when in roaming and light client modes is selected randomly, but can be changed manually in the UI, or using the new set API Proxy Peer API, or forced to a specific peer using the nxt.forceAPIProxyServerURL property.
Remote nodes can be blacklisted from the UI, or using the Blacklist API Proxy Peer API. This blacklisting is independent from peer blacklisting. The API proxy blacklisting period can be set using the nxt.apiProxyBlacklistingPeriod property (default 1800000 milliseconds).
API requests that require sending the secret phrase, shared key, or admin password to the server, for features like forging, shuffling, or running a funding monitor, are disabled when in roaming or light client mode.