Back to Nocobase

REST API data source

docs/docs/en/data-sources/data-source-rest-api/index.md

2.1.267.7 KB
Original Source

REST API data source

<PluginInfo commercial="true" name="data-source-rest-api"></PluginInfo>

Introduction

Use this data source to connect data provided by REST APIs.

Installation

This is a commercial plugin. For activation instructions, see the Commercial plugin activation guide.

Add a REST API source

After activating the plugin, choose REST API from the Add new menu in Data source management.

Configure the REST API source.

Add a collection

A RESTful resource maps to a NocoBase collection. For example, consider a Users resource:

bash
GET /users
POST /users
GET /users/1
PUT /users/1
DELETE /users/1

It maps to the following NocoBase API configuration:

bash
GET /users:list
POST /users:create
POST /users:get?filterByTk=1
POST /users:update?filterByTk=1
POST /users:destroy?filterByTk=1

For the complete NocoBase API design specification, see the API documentation.

See the NocoBase API - Core section.

The REST API data-source collection is configured as follows.

List

Configure the API mapping for retrieving a resource list.

Get

Configure the API mapping for retrieving resource details.

Create

Configure the API mapping for creating a resource.

Update

Configure the API mapping for updating a resource.

Destroy

Configure the API mapping for deleting a resource.

List and Get are required API mappings.

Debug APIs

Request parameter integration

For example, configure pagination parameters for the List API. If the third-party API itself does not support pagination, NocoBase paginates the returned list data.

Only variables added to the API take effect.

Third-party API parameterNocoBase parameter
page{{request.params.page}}
limit{{request.params.pageSize}}

Click Try it out to debug the API and inspect the response.

<video width="100%" height="440" controls> <source src="https://static-docs.nocobase.com/20241121211034.mp4" type="video/mp4"> </video>

Response format conversion

The response format of a third-party API might not match the NocoBase standard. Convert it before it can be displayed correctly in the client.

Adjust the conversion rules according to the third-party API response so that it matches the NocoBase output standard.

Debugging flow.

Error information conversion

When a third-party API returns an error, its error response might not match the NocoBase standard. Convert it before the client can display it correctly.

When error conversion is not configured, the error is converted by default to an error message that includes the HTTP status code.

After configuring error conversion to match the NocoBase output standard, the client can display third-party API error information correctly.

Variables

The REST API data source provides three variable categories for API integration:

  • Data-source custom variables
  • NocoBase requests
  • Third-party responses

Data-source custom variables

NocoBase requests

  • Params: URL query parameters (Search Params). The available Params differ between APIs
  • Headers: request headers, primarily providing NocoBase custom X- information
  • Body: the request body
  • Token: the API token of the current NocoBase request

Third-party responses

Currently, only the response Body is available.

The available variables for each API integration are as follows.

List

ParameterDescription
request.params.pageCurrent page number
request.params.pageSizeNumber of records per page
request.params.filterFilter conditions. They must use the NocoBase Filter format.
request.params.sortSorting rules. They must use the NocoBase Sort format.
request.params.appendsFields loaded on demand, typically for loading relationship fields on demand
request.params.fieldsFields that the API returns, as an allowlist
request.params.exceptFields to exclude, as a denylist

Get

ParameterDescription
request.params.filterByTkRequired. Usually the ID of the current record
request.params.filterFilter conditions. They must use the NocoBase Filter format.
request.params.appendsFields loaded on demand, typically for loading relationship fields on demand
request.params.fieldsFields that the API returns, as an allowlist
request.params.exceptFields to exclude, as a denylist

Create

ParameterDescription
request.params.whiteListAllowlist
request.params.blacklistDenylist
request.bodyInitial data for creating the record

Update

ParameterDescription
request.params.filterByTkRequired. Usually the ID of the current record
request.params.filterFilter conditions. They must use the NocoBase Filter format.
request.params.whiteListAllowlist
request.params.blacklistDenylist
request.bodyData to update

Destroy

ParameterDescription
request.params.filterByTkRequired. Usually the ID of the current record
request.params.filterFilter conditions. They must use the NocoBase Filter format.

Configure fields

Extract field metadata (Fields) from data returned by the adapted resource CRUD APIs and use it as the fields of the collection.

Extract field metadata.

Fields and preview.

Edit fields in the same way as for other data sources.

Add REST API data-source blocks

After configuring the collection, you can add blocks to pages.