All Products
Search
Document Center

Blockchain as a Service:Use REST APIs

Last Updated:Mar 31, 2026

The REST API lets you invoke smart contracts, query block and transaction data from the ledger, and subscribe to blockchain events. All requests require Bearer Token authentication. Include the following HTTP header with every API call:

Authorization: Bearer <your-access-token>

Prerequisites

Before you begin, ensure that you have:

  • A Blockchain as a Service instance with an organization configured

  • The Cloud Service Integration module installed. If not, follow the steps in Install Cloud Service Integration before proceeding.

Generate an access token

The REST API uses two tokens: an access token for authenticating requests, and a refresh token for obtaining a new access token when the current one expires.

  1. Go to your organization's REST API page.

    rest api入口

  2. Click Generate Token in the upper right corner. A sidebar appears. Select the validity period for the access token and refresh token, and check the permissions the token requires.

    image.png

  3. Click Generate Token in the sidebar. The generated token information appears in the text box.

Copy the access token — you'll use it to authenticate API calls and to configure Swagger UI.

Debug with Swagger UI

Swagger UI is available on the REST API page and lets you send test requests directly from the browser.

Configure the access token

To update the token used by Swagger UI — for example, when a token expires or you want to test a different token:

  1. Click Authorize on the page. A dialog showing the current authentication information appears.

    认证按钮

  2. If a token is already configured, click Logout to clear the existing authentication information.

    退出认证

  3. Enter your access token in the Value field, then click Authorize.

    输入Token

  4. Click Close. The lock icon next to each API becomes locked, confirming that authentication is configured.

    认证完成

Send a request

  1. Select the API you want to test — for example, invoke — and click its title to expand the details.

    API描述

  2. Click Try it out, then adjust the parameters as needed.

    API参数

  3. Click Execute. The response appears below the request.

    image.png

Important

The /networks/{network}/events/subscribe endpoint cannot be tested through Swagger UI. For the test method, see Subscribe to events.

Refresh an access token

Access tokens are short-lived. When a token is about to expire, use the refresh token to get a new access token without re-authenticating.

The REST API provides an OAuth 2.0-compliant refresh endpoint at /api/v1/token. Use a standard OAuth 2.0 SDK to handle token refresh automatically — find client implementations for each language at oauth.net/code.

Alternatively, use one of the SDK samples below. Each sample calls the notary contract by default. Download the notary sample contract before running the samples.

Java SDK sample

Requirements: Java 1.8 or later

  1. Get the Java SDK sample.

  2. Edit java-oauth-client/src/main/resources/application.properties and fill in your REST API address, token information, channel name, and smart contract information.

  3. From the java-oauth-client directory, run:

    mvn spring-boot: run

A successful run produces output similar to:

> mvn spring-boot:run
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.6.RELEASE)

2020-02-17 18:00:09.056  INFO 79141 --- [           main] com.aliyun.baas.MainApplication          : Starting MainApplication on Bright.local with PID 79141 (java-oauth-client/target/classes started by bright in java-oauth-client)
2020-02-17 18:00:09.059  INFO 79141 --- [           main] com.aliyun.baas.MainApplication          : No active profile set, falling back to default profiles: default
2020-02-17 18:00:09.092  INFO 79141 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5bf85360: startup date [Mon Feb 17 18:00:09 CST 2020]; root of context hierarchy
2020-02-17 18:00:09.775  INFO 79141 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2020-02-17 18:00:09.792  INFO 79141 --- [           main] com.aliyun.baas.MainApplication          : Started MainApplication in 0.958 seconds (JVM running for 3.321)
<200,class InlineResponse2002 {
    success: true
    result: class Block {
        number: 1
        hash: 1c397c4eb3e0e330c01ec430170f844e46159f16930aa347486b8153b6586548
        previousHash: 88ef0ad6ba2df7ba7e53de78575d2d14cee2253fe6897305e50b57ceeecebc78
        createTime: 1579056958
        transactions: []
        data: {data={data=[{payload={data={config={channel_group= ... }}}}]}}
    }
    error: class Error {
        code: 200
        message: Success
        requestId: edf8fe52-7cef-447f-a04a-7b8c1db56487
    }
},{Server=[nginx], Date=[Mon, 17 Feb 2020 10:00:10 GMT], Content-Type=[application/json; charset=UTF-8], Transfer-Encoding=[chunked], Connection=[keep-alive]}>
<200,class InlineResponse200 {
    success: true
    result: class Response {
        id: a5f5503f12b92a4c59e079e1baf49b517785e2d9988f90dfb234f6c3954a2389
        status: 200
        event: null
        data: MTU4MTkzMzYxMDEzNg==
    }
    error: class Error {
        code: 200
        message: Success
        requestId: 71a9f95f-ea5b-4dea-b4a1-a608ae429fb4
    }
},{Server=[nginx], Date=[Mon, 17 Feb 2020 10:00:11 GMT], Content-Type=[application/json; charset=UTF-8], Content-Length=[249], Connection=[keep-alive]}>
<200,class InlineResponse200 {
    success: true
    result: class Response {
        id: ba50180c9fe38c9be115f20775b78e80b7a1205c34ef34b66fab635efedc3b49
        status: 200
        event: null
        data: MTU4MTkzMzYxMDEzNg==
    }
    error: class Error {
        code: 200
        message: Success
        requestId: c703a36b-3589-4a8b-87a0-5e5bf56b2396
    }
},{Server=[nginx], Date=[Mon, 17 Feb 2020 10:00:11 GMT], Content-Type=[application/json; charset=UTF-8], Content-Length=[249], Connection=[keep-alive]}>
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.217 s
[INFO] Finished at: 2020-02-17T18:00:11+08:00
[INFO] ------------------------------------------------------------------------
2020-02-17 18:00:11.573  INFO 79141 --- [       Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5bf85360: startup date [Mon Feb 17 18:00:09 CST 2020]; root of context hierarchy
2020-02-17 18:00:11.574  INFO 79141 --- [       Thread-3] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

Go SDK sample

Requirements: Go 1.13.x

  1. Get the Go SDK sample.

  2. Edit the config in go-oauth-client/src/go-oauth-client/main.go and fill in your REST API address, token information, channel name, and smart contract information.

  3. From the go-oauth-client/src/go-oauth-client directory, run:

    go run main.go

A successful run produces output similar to:

> go run main.go
Block response body: {"Success":true,"Result":{"number":1,"hash":"1c397c4eb3e0e330c01ec430170f844e46159f16930aa347486b8153b6586548","create_time":1579056958,"previous_hash":"88ef0ad6ba2df7ba7e53de78575d2d14cee2253fe6897305e50b57ceeecebc78","transactions":[],"data":{"data":{"data":[{"payload":{"data":{"config":{"channel_group": ...}}}}]}}}
Invoke response body: {"Success":true,"Result":{"id":"e0a11c3b953fa1759ef715214bb8bd24c0a7e762b739eb5f645ead921314fef4","status":"200","events":[],"data":"MTU4MTkzNDAwOA=="},"Error":{"code":200,"message":"Success","request_id":"c3c92ad0-a0ef-4a82-b596-4ac50a893ef6"}}
Invoke contract response: "MTU4MTkzNDAwOA=="
Query response body: {"Success":true,"Result":{"id":"c3d4c2928dfa7129641863b18dfeee4b18c7227929c841ff7d8bd25148f6c5f6","status":"200","events":[],"data":"MTU4MTkzNDAwOA=="},"Error":{"code":200,"message":"Success","request_id":"f1bf52b2-8c60-491e-9a40-8fecb96667ea"}}
Query contract response: "MTU4MTkzNDAwOA=="

Node SDK sample

Requirements: Node.js 8.17 or later

  1. Get the Node SDK sample.

  2. Edit the config in node-oauth-client/main.js and fill in your REST API address, token information, channel name, and smart contract information.

  3. From the node-oauth-client directory, install dependencies and run the sample:

    npm install
    node main.js

A successful run produces output similar to:

> node main.js
{ number: 1,
  hash: '1c397c4eb3e0e330c01ec430170f844e46159f16930aa347486b8153b6586548',
  create_time: 1579056958,
  previous_hash: '88ef0ad6ba2df7ba7e53de78575d2d14cee2253fe6897305e50b57ceeecebc78',
  transactions: [],
  data:
   { data: { data: [Array] },
     header:
      { data_hash: 'HDl8TrPg4zDAHsQwFw+ETkYVnxaTCqNHSGuBU7ZYZUg=',
        number: '1',
        previous_hash: 'iO8K1rot97p+U954V10tFM7iJT/miXMF5QtXzu7OvHg=' },
     metadata: { metadata: [Array] } } }
Data 1581931486180 pushed to blockchain with transaction f19217c0db571dc715af8ad99025422f03e5561910371841fe5e69a356d0cb23
{ id: '8fd06f6087c5128b7dbe309658b170366b37e0733994e5e959d30be201c28827',
  status: '200',
  events: [],
  data: 'MTU4MTkzMTQ4NjE4MA==' }

Next steps

  • Subscribe to events — subscribe to blockchain events using the /networks/{network}/events/subscribe endpoint