Rust Amp Client (amp-rust)
⚠️ DEVELOPMENT STATUS: This package is currently in development and undergoing early integration testing. The API may change and some endpoints are not yet implemented. Don't use in production environments.
A Rust client for the Blockstream AMP API.
Usage
Add the following to your Cargo.toml:
[]
= "0.0.2"
Examples
For more detailed examples, please refer to the crate documentation.
Running Examples
You can run the included examples using cargo:
# Show a summary of all assets issued by your credentials
# View the API changelog
Make sure to set up your .env file with the required credentials first.
Get a registered user
use ApiClient;
async
Get an asset
use ApiClient;
async
Create a category
use ;
async
Manage asset assignments
use ApiClient;
async
Missing Endpoints
The following AMP API endpoints are not yet implemented in this client library. This list may not be exhaustive:
Asset Operations
POST /api/assets/{assetUuid}/reissue-request- Request asset reissuancePOST /api/assets/{assetUuid}/reissue-confirm- Confirm asset reissuancePOST /api/assets/{assetUuid}/burn-request- Request asset burnPOST /api/assets/{assetUuid}/burn-confirm- Confirm asset burnGET /api/assets/{assetUuid}/reissuances- Get asset reissuancesGET /api/assets/{assetUuid}/distributions/create/- Create asset distributionPOST /api/assets/{assetUuid}/distributions/{distributionUuid}/confirm- Confirm distributionDELETE /api/assets/{assetUuid}/distributions/{distributionUuid}/cancel- Cancel distributionGET /api/assets/{assetUuid}/distributions- Get asset distributionsGET /api/assets/{assetUuid}/distributions/{distributionUuid}- Get specific distributionGET /api/assets/{assetUuid}/txs- Get asset transactionsGET /api/assets/{assetUuid}/txs/{txid}- Get specific asset transactionGET /api/assets/{assetUuid}/lost-outputs- Get asset lost outputsPOST /api/assets/{assetUuid}/update-blinders- Update asset blinders
Manager Operations
POST /api/managers/{managerId}/change-password- Change manager password
These and potentially other endpoints will be added in future releases. If you need any of these endpoints urgently, please open an issue on the project repository.
Token Management
The AMP client includes sophisticated token management with automatic persistence and refresh capabilities.
Features
- Automatic Token Persistence: Tokens are automatically saved to
token.jsonand loaded on subsequent runs - Proactive Refresh: Tokens are automatically refreshed 5 minutes before expiry
- Thread-Safe Operations: All token operations are thread-safe and prevent race conditions
- Retry Logic: Built-in retry logic with exponential backoff for token operations
- Secure Storage: Tokens are stored securely using the
secrecycrate
Token Lifecycle
- First Run: Client obtains a new token from the API and persists it to disk
- Subsequent Runs: Client loads the existing token from disk if still valid
- Automatic Refresh: Token is automatically refreshed when it expires soon (within 5 minutes)
- Fallback: If refresh fails, client automatically obtains a new token
Usage Examples
use ApiClient;
async
Token Persistence Configuration
Token persistence is automatically enabled in the following scenarios:
- When
AMP_TESTS=live(for live API testing) - When
AMP_TOKEN_PERSISTENCE=trueis set - In test environments (
cfg!(test))
The token file (token.json) contains:
Configuration
Environment Variables
The client can be configured using the following environment variables:
Authentication (Required for live tests)
AMP_USERNAME: Username for AMP API authenticationAMP_PASSWORD: Password for AMP API authenticationAMP_API_BASE_URL: Base URL for the AMP API (default:https://amp-test.blockstream.com/api)
Retry Configuration (Optional)
API_RETRY_MAX_ATTEMPTS: Maximum number of retry attempts (default: 3)API_RETRY_BASE_DELAY_MS: Base delay between retries in milliseconds (default: 1000)API_RETRY_MAX_DELAY_MS: Maximum delay between retries in milliseconds (default: 30000)API_REQUEST_TIMEOUT_SECONDS: Request timeout in seconds (default: 10)
Test Configuration
AMP_TESTS: Set toliveto run tests against the actual API
Token Persistence (Optional)
AMP_TOKEN_PERSISTENCE: Set totrueto enable token persistence to disk (default: enabled for live tests)
Example Configuration
# Authentication
# Retry configuration (optional)
# Enable live tests
# Enable token persistence (optional)
Testing
To run the tests, you will need to set the AMP_USERNAME and AMP_PASSWORD environment variables.
AMP_USERNAME=... AMP_PASSWORD=... cargo test
To run the live tests, you will also need to set the AMP_TESTS environment variable to live.
AMP_USERNAME=... AMP_PASSWORD=... AMP_TESTS=live cargo test
Some tests that perform state-changing operations are ignored by default. To run them, use the --ignored flag.
AMP_USERNAME=... AMP_PASSWORD=... AMP_TESTS=live cargo test -- --ignored