# Create block
## Basic concept
- BlockEntity: A string of standard data structures. Including BlockID, SourceMeta, SourceData and other information. Stored on the open platform server. Blocks need to use BlockEntity data for rendering.
- [Creator](https://open.feishu.cn/document/uAjLw4CM/uYjL24iN/block/block-frame/logic-layer/register-creator) : The structure is similar to Block and is used to create BlockEntity.
- Host: The container that renders the Block.

## Block loading process

Before understanding the block creation process, you need to understand the block loading (rendering) process. As shown below:

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/8fe953c17102d2c4cc6b1e027f48b1b8_wnavUcKC73.png?height=839&lazyload=true&width=1640)

The `BlockEntity` needs to be used during the block loading process, and the developer decides how to render the block according to the `BlockEntity` data.

For example, if the business id is stored in `BlockEntity.sourceMeta`, the developer can initiate a request to obtain business data in the onLoad life cycle to complete the rendering. Of course, business data can also be stored in `BlockEntity.sourceData`, then developers can directly complete the rendering in the onLoad life cycle to avoid initiating network requests and complete rendering faster.

## Create method
The essence of Block creation is that developers use `Creator` to create a `BlockEntity` on the platform, and then insert a Block into the host.

The host uses the `Creator` to create a `BlockEntity` and saves the `BlockEntity.BlockID` to insert a `Block` somewhere on the page. When other people or the host opens the page again, the host will complete the block rendering of the area through `BlockID`.
After the BlockEntity is created, only `BlockEntity.sourceData` can be modified.

## Create BlockEntity

In the process of creating a block (for example: initiating creation in the cloud document plus menu), the host will first load the `Creator` provided by the developer. The developer calls `tt.setBlockInfo` in `Creator` to return a `BlockEntity` (either newly created or existing) to the host.

As explained in *Basic Concepts*, `BlockEntity` contains related data, such as business id, business data, etc. Therefore, there are two scenarios for the `BlockEntity` returned by `Creator` to the host:
- The developer server business id needs to be associated with `BlockEntity.blockID`.
- The developer server business id does not need to be associated with `BlockEntity.blockID`.

Therefore, there are two modes for creating `BlockEntity`: **developer front-end creation** and **developer server-side creation**.

### Developer front-end creation
#### Applicable scene

Developer front-end creation is applicable to the following scenarios, but not limited to the following scenarios:

1. A new business instance will be created every time it is created, but the business id does not need to be associated with `BlockID`.
2. Pure front-end Block, no developer server.

#### Create process
The front-end creation process of the developer is shown in the following figure: The developer only needs to complete the information collection for the creation of the BlockEntity, such as: the business ID bound to the business side of the block, setting the countdown time, and the style of the block.
If the developer server is not required to participate, you can remove the "developer service" related process in the flowchart. The entire operation of Block can be completely completed on the platform side, and the information is stored in `BlockEntity`. That is, developers only need to focus on Block **front-end** development.

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/2f86392b17df8436cc0cf35175b38325_3tBdazW7DI.png?height=864&lazyload=true&width=2110)

#### Sample code

```javascript
 Creator({
   onLoad() {
     const sourceData = { point: 1 }; // business data
     tt.request({
       url: 'https://[ServerDomain]/api/create', // developer server api address
       method: 'POST',
       data: {
         sourceData,
       },
       success(response) {
         // The developer server returns the generated business ID
         // The Block framework will create Blocks based on the information here
         tt.setBlockInfo({
           sourceMeta: {
             id: response.data.id,
           },
           sourceData,
         });
       }
     });
   },
 });
```

### Developer server creation (Not recommended)
#### Applicable scene
Developer backend creation is applicable to the following scenarios, but not limited to the following scenarios:

1. A new business instance will be created each time it is created, but the business id and `BlockID` need to be bound.
2. A business instance may not be generated each time it is created. You need to check whether the business id has been bound to `BlockID`.
In scenario 2, it is not necessary to create a new `BlockEntity`, or you can query the associated `blockID` and return it directly. For example, when developing a JiraBlock, `Creator` selects a Jira. When JiraId is associated with `BlockID` (the server side creation is completed), `tt.setBlockInfo` can be called directly, without the need for the developer server side to create a new `BlockEntity`.
#### Create process
In some scenarios, the developer server needs to perceive the created Block instance and establish a corresponding relationship with it. As shown in the figure below, the platform side provides a mode created by the developer server.

After the developer front-end (creator) collects enough information, it calls the creation api of the developer server, and the developer server calls the [Create BlockEntity](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/block-v2/entity/create) provided by the platform /create) completes the creation of the Block instance.

After the developer server completes the creation, it returns the blockID to the developer front end. Different from the developer front-end creation mode, the developer only needs to set a `blockID` when calling the `tt.setBlockInfo` API. The host will obtain the complete `BlockEntity` according to this `blockID` to complete the subsequent loading process.

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/7e27c6d67b63c2e87fabfa23278456a9_ul7aKD4iyG.png?height=870&lazyload=true&width=2140)

#### Sample code

````javascript
Creator({
  onLoad() {
    const sourceData = { point: 1 }; // business data
    tt.request({
      url: 'https://[ServerDomain]/api/create', // developer server api address
      method: 'POST',
      data: {
        sourceData,
      },
      success(response) {
        // The developer server returns the blockID
        tt.setBlockInfo({
          blockID: response.data.blockID,
        });
      }
    });
  },
});
````

#### Server API

For details, refer to [Server Creation Interface](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/block-v2/entity/create).