Public API

Updated at:
Copy as MD

The public API template helps you create an API service that can be invoked directly without authentication or an SDK. After creation, the service cannot be changed to require AppKey-based authentication. This API is designed for scenarios that require a simple way to expose IoT capabilities.

Prerequisites

You have created a project. For more information, see project.

Create a public API service

Important
  • The following nodes cannot be configured in a public API service: product-specific device nodes, MQTT publish, mobile app push, application push, intra-project API, variable setting, and key-value pair operations. These nodes require authentication. If you call the public API directly from an external source (other than the web-based visual development environment), you cannot retrieve the output data from these nodes.

    Alternatively, you can create a blank business service and configure it as an API that requires authentication. When calling such an API externally, the required SDK handles node authentication.

  • External applications can directly call the HTTP-based public API to retrieve its response data. If this data contains sensitive information, it poses a serious risk of a data leak. To ensure data security, configure and use the public API with caution.
  1. In the common project list, find the target project and click its project card.

    You can also click Global Resource Project to view its details.

  2. On the Home page of the project, select the Business Logic tab.
  3. In the upper-left corner of the business service list, click New and select New from Template.
  4. On the Business Logic Development page, click Expand More Templates in the upper-right corner.
  5. Find and click the Public API template card.
  6. On the Create Business Logic from Template pane on the right, configure the basic information for the service.
    Parameter Description
    Service Name Enter a custom name for the service.

    The name can be up to 30 characters long, must start with a letter, digit, or Chinese character, and can only contain letters, digits, Chinese characters, underscores (_), hyphens (-), and parentheses (()).

    Project The project to which the service belongs.
    Description Enter a description of the service's purpose, up to 100 characters long.
  7. Click Create with This Template.
    After the business service is created, you are redirected to the service editor. The system automatically generates a business flow that connects an HTTP request node, a Node.js script node, and an HTTP response node. You can configure the node parameters as needed.

Edit the service flow

  1. On the service editor canvas, click the HTTP request node and set a value for the Action parameter, such as GetValue.
    For more parameter settings, see HTTP request.
  2. Click the script node to configure its settings.
    This example uses the script from the blank template. For more details on configuring the script node, see Node.js script. The script is an asynchronous function that accepts four parameters: payload (the output from the previous node), node, query, and context. By default, the code calls console.log to print the payload value and returns the string 'hello world'.
  3. Click the HTTP response node to configure the return value of the API service.
    This example directly returns the value from the previous node (payload), which is 'hello world'. For more configuration options, see HTTP response. In the Output area, select From Node and then choose Previous Node (payload) from the drop-down list.
  4. In the upper-right corner of the page, click the Save button保存.

Debug and publish

  1. On the service editor page, click the Deploy & Debug button部署 in the upper-right corner to deploy the service.
  2. After the service is deployed successfully, click the Deploy & Debug button部署 again to debug the service.
    After debugging is complete, a green checkmark appears on all nodes, indicating that they ran successfully. In the Node Log panel on the right, you can view the execution details for each node. In this example, the final output is 'hello world'.
  3. Click the Publish button to the right of the Debug button to publish the service.
    A business service must be published to the cloud before it can be called. After a successful publication, a success message appears. You can select How to use service, Back to workspace, or Back to project overview.

Calling the published API

This example demonstrates how to call the published API directly using a curl command.

  1. On the far left of the page, click the Service Invocation Settings button设置.
  2. On the API Invocation Method tab, get the API Path value for the POST method (for example, http://*.com) and the default value for Action (for example, GetValue).
  3. Open your system's command prompt or terminal and enter the curl command.
    Note The ActionGetValue value and the API Pathhttp://*.com value in the command are only examples. Replace them with the data from the previous step.
    curl -v -X POST -d "{'params':{'action':'GetValue'},'request':{'apiVer':'1.0.0'},'version':'1.0','id': 12}" http://*.com
  4. Press Enter to run the command and get the API response.
    {"code":200,"data":"hello world","success":true,"description":"Request successful","id":"2e8fee80-***-***","message":"success"}

For more ways to call the API, see the Request Example section under the API Invocation Method tab.