thirdweb Engine
repository·main·Indexed 42 days ago
https://github.com/thirdweb-dev/engineAn open-source backend HTTP server providing a production-ready interface to read, write, and deploy contracts on EVM blockchains and private subnets. It features high throughput (700+ TPS), managed backend wallets (local or KMS), gas optimization, smart account support with session tokens, gasless transactions via relayers, and webhook support.
What's inside thirdweb Engine
- thirdweb Engine is an open-source, backend HTTP server designed to provide a production-ready interface for interacting with blockchain contracts. It allows developers to read, write, and deploy contracts across various EVM blockchains and private subnets. Key capabilities include high throughput (700+ transactions per second), managed backend wallets (local or KMS), gas optimization (retries, ceilings, and timeouts), smart account support with session tokens, gasless transactions via relayers, and webhook support for wallets and contracts.
Set up thirdweb Engine
mainYou can deploy and use thirdweb Engine through several methods depending on your needs:
Managed Hosting
Get Engine hosted and managed by thirdweb for a hands-off experience via the thirdweb Dashboard.
Self-Hosting
- Free Self-hosting: Follow the official self-host instructions to run Engine on your own infrastructure.
- Railway: Use the Railway template for a quick deployment on Railway.
Marketplace V3 API Routes Overview
mainThe Marketplace V3 extension provides a suite of API routes for managing digital assets through three primary mechanisms: Direct Listings, English Auctions, and Offers. These routes allow developers to read marketplace state (listings, auctions, offers) and perform write operations (creating listings, making bids, accepting offers).Understand Engine's nonce management concepts
mainEngine tracks nonces using several distinct states to ensure reliable transaction sequencing and to prevent nonce collisions or gaps:
- On-chain Nonce: The state of the blockchain. It is calculated as the
transactionCountfrom the RPC minus 1 (representing the last successfully mined transaction). - Last Used Nonce: The highest nonce that Engine has attempted to use and sent to the RPC.
- Sent Nonces: A queue of nonces that have been broadcast to the network but have not yet been included in a block (mined).
- Recycled Nonces: A pool of nonces that were reserved by Engine but failed to be successfully broadcast to the RPC. These are held in a pending state to be either retried (recycled) or cancelled to prevent gaps in the transaction sequence.
- On-chain Nonce: The state of the blockchain. It is calculated as the
Configure deployment headers and transaction modes
mainWhen calling deployment methods, you can provide several optional headers to control the transaction behavior:
xIdempotencyKey: Prevents duplicate transactions. Engine compares the last 100,000 transactions to de-duplicate.xTransactionMode: Set to'sponsored'for sponsored transactions. This is ignored if using a smart wallet.xAccountAddress: The smart account address.xAccountFactoryAddress: The smart account factory address. If omitted, Engine attempts to resolve it from the contract.xAccountSalt: A string or hex salt used to predict the smart account address. Useful when creating multiple accounts with the same admin.
Understand the ABI schema structure
mainThe engine uses
abiSchemaandabiTypeSchemato validate ABI definitions. An ABI is an array of objects where each object represents an entry (function, event, etc.).Key fields in
abiTypeSchemainclude:type(Optional String)name(Optional String)internalType(Optional String)components(Optional Array of objects withtype,name, andinternalType)
Key fields in
abiFunctionSchemainclude:name(String)inputs(Array ofabiTypeSchema)outputs(Array ofabiTypeSchema)signature(String)stateMutability(String)comment(Optional String)
Understand Public Endpoints and Sandbox Mode
mainCertain routes are considered public and do not require authentication. These include:
GET /GET /favicon.icoGET /system/healthGETroutes matching OpenAPI definitionsGET /auth/user*GET /transaction/status*POST /auth/payload,/auth/login,/auth/switch-account,/auth/logout/relayer/{uuid}(Relay transaction endpoints)- Admin queue routes (which enforce their own specific auth)
Additionally, if the
ENGINE_MODEenvironment variable is set tosandbox, the authentication check is skipped entirely for all requests.Understand the structure of an NFT
mainThe
nftSchemadefines the complete structure of an NFT as tracked by the engine. It includes:metadata: ThenftMetadataSchema(id, uri, name, description, etc.).owner: The wallet address of the owner.type: The contract standard, which must be one ofERC1155,ERC721, ormetaplex.supply: The total supply as a string.quantityOwned: (Optional) The amount owned by the user.
Understand the CustomError structure
mainThe engine uses a specific
CustomErrortype to handle expected operational errors. When you throw an object matching this shape, the middleware will catch it and return it as a structured JSON response.Type Definition:
type CustomError = { message: string; statusCode: number; code: string; stack?: string; };API Response Format: When a
CustomErroris thrown, the client receives a response with the following JSON body:{ "error": { "code": "STRING_CODE", "message": "Human readable message", "statusCode": 400, "stack": "..." } }Enable Keypair Authentication
mainKeypair authentication allows requests to be authenticated using a JWT signed by an ES256 private key that matches a configured public key.
To use this feature, you must:
- Set the
ENABLE_KEYPAIR_AUTHenvironment variable totrue. - Ensure the public key is added to Engine.
- (Optional) If the JWT contains a
bodyHash, the request body must match the SHA256 hash provided in the token to prevent tampering.
- Set the
Configure IP Allowlisting for Security
mainEngine supports IP allowlisting to restrict access to specific IP addresses.
- If the
ipAllowlistin your configuration is empty, all IPs are allowed. - If configured, the middleware checks the request IP against the list. If
TRUST_PROXYis enabled (or ifENGINE_TIERis set), the middleware will use thecf-connecting-ipheader (Cloudflare) to identify the client IP. - If an IP is not in the allowlist, the request is rejected with a
401 Unauthorizederror.
- If the
Configure authentication with `withAuth`
mainThe
withAuthfunction is a Fastify middleware used to set up authentication and permission handling in an Engine server. It integrates the@thirdweb-dev/auth/fastifyplugin, registers an/authrouter, and adds apreValidationhook to validate incoming requests.When configured, it allows for several authentication patterns:
- Access Tokens: Non-revoked tokens for Owners or Admins.
- Keypair Authentication: JWTs signed by an ES256 private key (requires
ENABLE_KEYPAIR_AUTHenvironment variable). - Dashboard Auth: JWTs issued by the Thirdweb Dashboard.
- Secret Key: Using the
THIRDWEB_API_SECRET_KEYin theAuthorizationheader. - Auth Webhooks: External verification via configured webhooks.
In non-sandbox mode, if authentication fails, the middleware returns a
401 Unauthorizedresponse with an error message.import { withAuth } from './path/to/auth'; // Inside your Fastify server setup await withAuth(server);