pairecmd is the command-line tool for scaffolding PAI-Rec recommendation engine projects. This guide walks you through downloading pairecmd, creating a project, compiling and packaging it, running the service locally, and testing the built-in recommendation API.
Prerequisites
Before you begin, make sure you have:
Go installed and
GOPATHconfiguredDocker installed (required to build and push container images)
An Alibaba Cloud Container Registry instance (required for the image packaging step)
Download pairecmd
Download the binary for your platform:
| Platform | Download link |
|---|---|
| Linux | pairecmd |
| macOS (Intel) | pairecmdmac |
| macOS (Apple Silicon) | pairecmdmacarm |
| Windows | pairecmdwin |
On Linux or macOS, grant execute permission after downloading. Replace <binary> with the downloaded filename:
chmod +x <binary>Version 2 of pairecmd supports the PAI-Rec console. For production projects, use the console to manage your project configuration.Create a project
Run the following command to create a project named pairec-demo. The command generates a pairec-demo folder containing the project scaffold in the current directory.
./pairecmdmac project --name pairec-demoThe generated folder has the following structure:
pairec-demo
├── Makefile
├── conf
│ └── config.json.production
├── docker
│ └── Dockerfile
├── go.mod
└── src
├── controller
│ └── feed.go
└── main.goNavigate into the project folder and install dependencies:
cd pairec-demo
go mod tidyCompile and package the project
The project includes a Makefile with targets for compiling and packaging.
Compile the project:
make && make buildIf the compilation fails, run go mod tidy first to resolve any missing dependencies.
Package the project into a container image:
make releaseBefore running make release, activate a personal image in Alibaba Cloud Container Registry and create the image repository referenced by BIN_NAME in the Makefile.

Run the project
Navigate to the pairec-demo folder and start the service. Use the config parameter to specify the configuration file path. Logs are printed to the terminal.
go run src/main.go --config=conf/config.json.production --alsologtostderrTo verify the service is running, open another terminal and send a test request:
curl -v http://localhost:8000/api/rec/feed -d '{"uid":"76295990", "size":10, "scene_id":"home_feed"}'A successful response returns HTTP 200 with a JSON body similar to the example in the Request and response example section below.
For details on the config parameter options, see Engine configuration.
API reference
The generated project includes a built-in recommendation API. The implementation is in controller/feed.go.
Endpoint
POST /api/rec/feedRequest parameters
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
uid | string | Yes | User ID | "1000079" |
size | integer | Yes | Number of items to retrieve | 10 |
scene_id | string | Yes | Scenario ID | home_feed |
features | JSON object | No | Context features as key-value pairs | {"age":20, "sex":"male"} |
complex_type_features | JSON array | No | Context features with explicit type information, required when sending typed data to the model service | [{"name":"age", "type":"int", "values":20}] |
item_id | string | No | Item ID for similarity-based recommendations | 248791390 |
item_list | JSON array | No | Custom recall list | [{"item_id":"1111", "score":1}] |
debug | boolean | No | Enable debug mode to print additional logs | true |
request_id | string | No | Custom request ID. If omitted, the PAI-Rec engine generates one automatically | "c46c3f5e-6b32-4b5c-8aac-59a319941248" |
`item_list` behavior
item_list is a JSON array of objects. Each object must include an item_id field. If an object includes a score field, that value is used as the recall score. All other fields are treated as item properties.
`complex_type_features` format
Pass complex context features as an array. Each element has the following fields:
| Field | Description | Example |
|---|---|---|
name | Context feature name | age |
type | Context feature type | int |
values | Context feature value — supports scalar values, arrays, and maps | 20 |
Supported types: int, int64, float, double, string, list<int>, list<int64>, list<float>, list<double>, list<string>, map<int,int>, map<int,int64>, map<int,float>, map<int,double>, map<int,string>, map<string,int>, map<string,int64>, map<string,float>, map<string,double>, map<string,string>, map<int64,int>, map<int64,int64>, map<int64,float>, map<int64,double>, and map<int64,string>.
Examples:
Single value:
[{"name":"age", "type":"int", "values":20}, {"name":"sex", "values":"male", "type":"string"}]Array:
[{"name":"list_features", "type":"list<int>", "values":[1,2,3]}]Map:
[{"name":"map_features", "type":"map<int,int>", "values":{"1":10,"2":20,"3":30}}]
complex_type_features and features can be used together in the same request.
Request and response example
Request:
curl -v http://host/api/rec/feed -d '{"uid":"76295990", "size":10, "scene_id":"home_feed"}'Response:
{
"code": 200,
"msg": "success",
"request_id": "e332fe9c-7d99-45a8-a047-bc7ec33d07f6",
"size": 10,
"experiment_id": "",
"items": [
{
"item_id": "248791390",
"score": 0.9991594902203332,
"retrieve_id": "mock_recall"
}
]
}Response fields
| Field | Type | Description | Example |
|---|---|---|---|
code | integer | Business status code | 200 |
msg | string | Status message | success |
request_id | string | Unique request ID | e332fe9c-7d99-45a8-a047-bc7ec33d07f6 |
size | integer | Number of recommended items returned | 10 |
experiment_id | string | Experiment ID. Empty string if no A/B testing is active | ER2_L1#EG1#E2 |
items | JSON array | List of recommended items | — |
Each item in items contains:
| Field | Type | Description | Example |
|---|---|---|---|
item_id | string | Recommended item ID | 3v5RE7417j7R |
retrieve_id | string | Recall source ID | u2i_recall |
score | float | Recommendation score | 0.45 |
extra | JSON object | Custom output fields. Not returned unless configured | — |
Error codes
| Code | Description | msg |
|---|---|---|
200 | Request succeeded | success |
299 | Insufficient items returned | items size not enough |
400 | Parameter error | uid not empty or unexpected end of JSON input |
500 | Server error | HTTP status code returned |
Custom output fields
By default, each item in the response includes item_id, retrieve_id, and score. To return additional fields — such as item properties or model scores — configure OutputFields under SceneConfs for the target scenario. The API returns the configured fields in the extra field.
Example configuration:
"SceneConfs": {
"${scene_name}": {
"default": {
"RecallNames": [
"collaborative_filter"
],
"OutputFields": [
"item:type",
"item:age",
"score:*",
"score:model_v1_ctr"
]
}
}
}Fields prefixed with
item:return the corresponding item property. If the property does not exist,nullis returned.Fields prefixed with
score:return the score from the specified model.score:*returns all model scores, placed inalgo_scores.
FAQ
How do I pass an item list through the API for reranking?
Pass the list using the item_list parameter. In the engine configuration, set up a Context Item Recall (ContextItemRecall) so the PAI-Rec engine reads items directly from item_list.
How do I instrument the client to use experiment reports?
The API response includes request_id and experiment_id. Record both fields in your impression and click behavior logs at a minimum. Including them in additional logs — such as playback duration, add-to-cart events, and purchases — gives more complete performance data.
Once your behavior logs are instrumented, generate the offline Experiment Report Source Table and register it with the AB Test platform. Configure custom metrics as needed, then view results in the Experiment Metrics Report. For the SQL pattern used to generate the source table, see Data Registration and Field Configuration.
What's next
Engine configuration — configure recall, ranking, and filtering pipelines
Data Registration and Field Configuration — register user and item data
Experiment Metrics Report — analyze A/B testing results