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

寻求从托管网站调用Hyperledger Playground生成的REST API的分步教程

Great question! You absolutely can leverage the REST API generated from Hyperledger Playground in a hosted web app (replacing MySQL) — here's a detailed, step-by-step guide for non-local access:

Step 1: Move Beyond Local Hyperledger Playground

By default, Hyperledger Playground runs against a local, ephemeral Fabric instance. For remote access, you’ll need to deploy your business network to a persistent, remote Hyperledger Fabric cluster:

  • You can use managed services like IBM Blockchain Platform, or set up your own remote Fabric nodes on cloud servers like AWS, GCP, or Azure.
  • Export your business network from Playground as a .bna file (use the "Export" option in the Playground interface).
Step 2: Deploy Your Business Network to the Remote Fabric
  1. Install the Hyperledger Composer CLI tools on your local machine (or a deployment server) if you haven’t already:
    npm install -g composer-cli composer-rest-server
    
  2. Create a connection profile that points to your remote Fabric cluster (this file defines peer, orderer, and certificate authority endpoints).
  3. Import an admin identity card for your remote Fabric, then install and start your .bna business network:
    composer network install -c admin@remote-fabric -a your-business-network.bna
    composer network start -c admin@remote-fabric -n your-business-network -V 0.0.1 -A admin -S adminpw
    
  4. Import the business network card to interact with the deployed network:
    composer card import -f admin@your-business-network.card
    
Step 3: Host the Composer REST Server Remotely

The REST API you use will be served by the Composer REST Server, hosted on a publicly accessible server:

  1. On your remote server, install the Composer REST Server:
    npm install -g composer-rest-server
    
  2. Start the server with configurations that enable remote access and cross-origin requests (critical for hosted web apps):
    composer-rest-server -c admin@your-business-network -n never -w true -a true -t true
    
    Let’s break down these flags:
    • -c: Specifies the business network card to use
    • -n never: Disables TLS for testing (enable it in production with -t true and provide certificates)
    • -w true: Enables anonymous access (replace with OAuth2/JWT in production for security)
    • -a true: Enables CORS (cross-origin resource sharing) so your hosted web app can call the API
    • -t true: Enables TLS encryption for secure production traffic
Step 4: Secure the API for Production

If you’re deploying to production, don’t skip these steps:

  • Replace anonymous access with OAuth2 or JWT authentication (use the -o true flag when starting the REST Server to enable OAuth, then connect to your identity provider like Auth0 or Keycloak).
  • Use valid TLS certificates (letsencrypt works for free) to encrypt traffic between your web app and the REST Server.
  • Restrict CORS origins to only your hosted web app’s domain (instead of -a true, use -o https://your-web-app.com).
Step 5: Integrate the REST API into Your Hosted Web App

Now you can replace MySQL calls with the Hyperledger REST API endpoints. Here’s a quick example using JavaScript’s fetch API:

// Example: Fetch all assets from the business network (replace with your asset type)
async function fetchAssets() {
  try {
    const response = await fetch('https://your-rest-server-domain.com/api/YourAssetType', {
      method: 'GET',
      headers: {
        'Authorization': 'Bearer YOUR_JWT_TOKEN', // Include this if using authentication
        'Content-Type': 'application/json'
      }
    });
    const assets = await response.json();
    // Use these assets just like you would data from MySQL
    renderAssetList(assets);
  } catch (error) {
    console.error('Failed to fetch assets:', error);
  }
}

// Example: Create a new asset (replace with your asset structure)
async function createAsset(newAssetData) {
  try {
    const response = await fetch('https://your-rest-server-domain.com/api/YourAssetType', {
      method: 'POST',
      headers: {
        'Authorization': 'Bearer YOUR_JWT_TOKEN',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(newAssetData)
    });
    const createdAsset = await response.json();
    console.log('Asset created:', createdAsset);
  } catch (error) {
    console.error('Failed to create asset:', error);
  }
}
  • Map your app’s CRUD operations to the REST API’s endpoints:
    • Read: GET /api/AssetType or GET /api/AssetType/{id}
    • Create: POST /api/AssetType
    • Update: PUT /api/AssetType/{id}
    • Delete: DELETE /api/AssetType/{id}
Step 6: Test the End-to-End Flow
  1. Use Postman or curl to test all REST API endpoints first, ensuring they work with your remote Fabric network.
  2. Test your web app’s integration thoroughly: create, read, update, and delete assets to confirm data persists correctly in the Fabric ledger.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:30:45