You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从CLI将钱包导入Composer REST服务器并实现自动化导入?

Automating .card File Imports into Composer REST Server via Shell Script

Got it, I’ve helped a few folks automate this exact workflow for demos and streamlined network setups. Instead of manually using the POST /wallet/import endpoint through the UI, we can replicate that form upload behavior directly in a shell script with curl. Here’s a step-by-step solution:

Core Concept

When you upload a .card file via the REST UI, it sends a multipart/form-data POST request with two key parts:

  • The .card file itself (attached under the card field)
  • A name parameter to label the wallet entry

We can mirror this behavior in a script to loop through all your .card files and import them automatically.

Shell Script Implementation

Here’s a reusable script you can integrate into your network setup workflow. It assumes all your .card files live in a single directory (adjust paths as needed):

#!/bin/bash

# Configuration - update these values to match your environment
REST_SERVER_URL="http://localhost:3000"  # Base URL of your Composer REST Server
CARD_DIRECTORY="./network-cards"        # Path to folder containing .card files

# Validate the card directory exists
if [ ! -d "$CARD_DIRECTORY" ]; then
    echo "Error: Card directory '$CARD_DIRECTORY' doesn't exist. Check your path."
    exit 1
fi

# Loop through every .card file in the directory
for card_file in "$CARD_DIRECTORY"/*.card; do
    # Skip if no .card files are found in the directory
    [ -e "$card_file" ] || continue

    # Use the filename (without .card extension) as the wallet entry name
    wallet_name=$(basename "$card_file" .card)

    echo "Starting import for $card_file as wallet entry '$wallet_name'..."

    # Send the POST request to import the card
    curl -X POST \
         -F "card=@$card_file" \
         -F "name=$wallet_name" \
         "$REST_SERVER_URL/wallet/import"

    # Add spacing between import logs for readability
    echo -e "\n---"
done

echo "All card imports completed successfully!"

Key Customizations & Tips

  • Authentication: If your REST server uses basic auth, add --user your-username:your-password to the curl command. For token-based auth, include -H "Authorization: Bearer YOUR_AUTH_TOKEN".
  • Custom Wallet Names: If you don’t want to use the filename as the wallet name, you can hardcode names or create a CSV mapping file (linking filenames to custom labels) and read from it in the loop.
  • Error Handling: To make the script more robust, add -w "%{http_code}" to the curl command to capture HTTP response codes, then log failures if the code isn’t 200.
  • CORS Considerations: If running the script against a remote REST server, ensure the server has CORS enabled for your script’s origin (or run the script directly on the server hosting the REST API to avoid CORS issues).

How to Use

  1. Save the script as import-wallet-cards.sh
  2. Make it executable: chmod +x import-wallet-cards.sh
  3. Place all your .card files in the specified CARD_DIRECTORY
  4. Run the script: ./import-wallet-cards.sh

This will handle all imports automatically, eliminating the need for manual UI interactions—perfect for speeding up demos or automated network provisioning.

内容的提问来源于stack exchange,提问作者Simon Mullaney

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:23:48