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

如何扩展Hyperledger Composer REST Server自定义API及导出Swagger定义?

Hey there! Let's break this down step by step since you're running a Hyperledger Fabric test instance via IBM Blockchain Application Service, with a Hyperledger Composer REST Server deployed on Kubernetes, and want to extend it with custom APIs while figuring out how to export the Swagger definition.

第一步:导出Composer REST Server的Swagger定义

Hyperledger Composer REST Server (built on LoopBack) exposes its Swagger definition out of the box—you just need to know where to look:

  • Option 1: Grab it from the Swagger UI
    Your REST Server comes with a built-in Swagger explorer at http://<your-rest-server-ip>:<port>/explorer. Open that in your browser, and look for an "Export" button (usually at the top-right or near the API title). You can download the definition as either a YAML or JSON file directly from here.
    Note: If your REST Server uses authentication (like OAuth2), you'll need to log in first via the UI before you can access the export option.

  • Option 2: Use curl to fetch the raw definition
    The REST Server exposes dedicated endpoints for the Swagger spec. Run one of these commands (replace the IP/port with your server's details):

    # Fetch as JSON
    curl http://<your-rest-server-ip>:<port>/swagger.json > composer-swagger.json
    
    # Fetch as YAML
    curl http://<your-rest-server-ip>:<port>/swagger.yaml > composer-swagger.yaml
    

    If authentication is enabled, add your auth token to the request:

    curl -H "Authorization: Bearer <your-auth-token>" http://<your-rest-server-ip>:<port>/swagger.yaml > composer-swagger.yaml
    
第二步:扩展自定义API(两种主流方案)

Now that you have the Swagger definition, you can extend your API using either IBM API Connect or directly via LoopBack (since the Composer REST Server is a LoopBack app):

方案A: 使用IBM API Connect

  1. Import the Swagger definition
    In your IBM API Connect instance, create a new API and select the option to "Import an API from a file". Upload the Swagger YAML/JSON you exported earlier—this will bring in all your existing Composer REST endpoints.
  2. Add your custom APIs
    Use the API Connect editor to add new paths, operations, and logic to your imported API. For example, you could add a POST /api/custom-transaction endpoint that orchestrates multiple Composer transactions, or integrates with external services. You can use API Connect's assembly tools to define how the custom endpoint processes requests (like transforming data, calling backend services, or invoking chaincode indirectly).
  3. Publish the extended API
    Once you're done editing, publish the API to your API Connect gateway. Your clients will now access all original Composer endpoints plus your custom APIs through the API Connect gateway URL.

方案B: 直接扩展LoopBack(更原生的方法)

Since the Composer REST Server is built on LoopBack, you can extend it directly by adding custom remote methods or models—this avoids needing to export/import Swagger separately, and your custom APIs will automatically appear in the Swagger UI.

  1. Access the REST Server codebase
    If you deployed the REST Server via Kubernetes, you'll need to either:
    • Mount a volume with custom code into your deployment, or
    • Fork the Composer REST Server repo, add your custom code, and rebuild the container image for your K8s deployment.
  2. Add a custom remote method
    Create a new JavaScript file (e.g., custom-api.js) in the LoopBack app's server/boot directory, and define your custom API using LoopBack's remoteMethod API. For example:
    module.exports = function(app) {
      const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
    
      // Define a custom GET endpoint
      app.remoteMethod(
        'customApi.getCustomData',
        {
          http: { path: '/custom-data', verb: 'get' },
          returns: { arg: 'data', type: 'object' },
          description: 'Fetch custom data from Fabric chaincode'
        }
      );
    
      // Implement the endpoint logic
      app.customApi.getCustomData = async function() {
        const connection = new BusinessNetworkConnection();
        await connection.connect('<your-business-network-card>');
        const result = await connection.query('<your-custom-query>');
        await connection.disconnect();
        return result;
      };
    };
    
  3. Restart the REST Server
    In Kubernetes, this means rolling out a new deployment with your updated code/image. Once restarted, your custom API will show up in the Swagger UI at /explorer, and will be included in the Swagger definition at /swagger.yaml.
关键注意事项
  • Authentication: Make sure your custom APIs enforce the same authentication rules as your existing Composer REST endpoints (e.g., OAuth2 tokens) to maintain security.
  • Kubernetes Deployment: If using volume mounts for custom code, ensure your K8s pod has read access to the mounted files. If rebuilding images, tag them properly and update your deployment to use the new image.
  • Chaincode Interaction: When writing custom APIs that interact with Fabric, use the composer-client library to maintain consistency with the Composer REST Server's chaincode access patterns.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:32:13