This topic describes how to call the API after you publish a service.
API description
POST /api/rec/feedRequest parameters
Parameter Name | Description | Type | Required | Example |
uid | The user ID. | string | Yes | "1000079" |
size | The number of recommended items to return. | integer | Yes | 10 |
scene_id | The recommendation scenario ID. This ID distinguishes requests for different recommendation scenarios. | String | Yes | home_feed |
features | Contextual features. | json map | No | {"age":20, "sex":"male"} |
complex_type_features | Contextual features of complex types. Set this parameter to send type information to the model service. | json array | No | [{"name":"age", "type":"int", "values":20}, {"name":"sex", "values":"male", "type":"string"}] |
item_id | The item ID for similarity recommendations. | string | No | 248791390 |
item_list | A custom list for retrieval. | json array | No | [{"item_id":"1111", "score":1},{"item_id":"222", "score":0.95}] |
debug | Used for debugging. When enabled, more logs are printed. For example, user and item details are printed in the console. If traffic control is enabled, the traffic control process and changes to certain calculated parameters are printed in the results. | bool | No | true |
request_id | The unique ID of the request. If this parameter is empty, the PAI-Rec engine automatically generates one. If you provide a value, that value is used as the request ID. | string | No | "c46c3f5e-6b32-4b5c-8aac-59a319941248" |
item_list
If you have a batch of retrieved data to pass to the API, assign it to the item_list parameter. The item_list parameter is an array where each item is a map object. Each item must contain an item_id field, which represents the item ID. All other fields are optional. If an item contains a score field, its value is treated as the retrieval score. Other fields are treated as item properties.
complex_type_features
Contextual features of complex types must be passed as an array. Each element contains the following fields:
Name | Description | Example |
name | The name of the contextual feature. | age |
type | The type of the contextual feature. | int |
values | The value of the contextual feature. You can set various types, such as specific values, arrays, and maps. | 20 |
Examples:
Pass a single value:
[{"name":"age", "type":"int", "values":20}, {"name":"sex", "values":"male", "type":"string"}]Pass an array:
[{"name":"list_features", "type":"list<int>", "values":[1,2,3]}]Pass a map:
[{"name":"map_features", "type":"map<int,int>", "values":{"1":10,"2":20,"3":30}}], or[{"name":"map_features", "type":"map<int,int>", "values":{"1":"10","2":"20","3":"30"}}]
The type parameter supports the following 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>.
Note: You can use the complex_type_features and features parameters at the same time to pass contextual information.
Response
Name | Description | Type | Example |
code | The business code returned by the API. | int | 200 |
msg | The business information. | string | success |
request_id | The unique ID of the request. | string | e332fe9c-7d99-45a8-a047-bc7ec33d07f6 |
size | The number of recommended items returned. | int | 10 |
experiment_id | The experiment ID. If you are not using A/B testing, this parameter is empty. | string | ER2_L1#EG1#E2 |
items | A list of recommended items. | json array | [{"item_id":"248791390","score":0.9991594902203332,"retrieve_id":"mock_recall"}] |
The `items` are as follows:
Name | Description | Type | Example |
item_id | The ID of the recommended item. | string | 3v5RE7417j7R |
retrieve_id | The ID of the retrieval source. | string | u2i_recall |
score | The recommendation score. | float | 0.45 |
Error codes
Error code | Description | msg |
200 | The API call is successful. | success |
299 | The number of returned items is insufficient. | items size not enough |
400 | A parameter is invalid. The msg value varies based on the specific error. | uid not empty or unexpected end of JSON input |
500 | A server error occurred. This is returned as an HTTP error code. |
Configuration and call examples
This section provides an example of contextual retrieval, where a retrieval list is passed to the API. The following code shows the engine configuration for the home_feed scenario:
{
"RunMode": "product",
"ListenConf": {
"HttpAddr": "",
"HttpPort": 8000
},
"SortNames": {
"default": ["ItemRankScore"]
},
"FilterNames": {
"default": ["UniqueFilter"]
},
"SceneConfs": {
"home_feed": {
"default": {
"RecallNames": ["ContextItemRecall"]
}
}
}
}The request body is as follows:
{
"uid":"13579",
"size":2,
"scene_id":"home_feed",
"item_list":[{"item_id":"1111", "score":1},{"item_id":"222", "score":0.95}]
}API calls
Call the API using a cURL command
To call the engine service from your local machine using a cURL command, first obtain the request URL and token for the service from the Elastic Algorithm Service (EAS) console, as shown in the following figure.
On the service details page, click View Endpoint Information to view the call information.

If you call the service from your local machine, use the public endpoint. The complete request URL is the public endpoint followed by /api/rec/feed.
curl -X 'POST' -v \
'http://12*****.vpc.hangzhou.pai-eas.aliyuncs.com/api/predict/test/api/rec/feed' \
-H 'Authorization:YmRjZThkM2Z*****************TU5Mw==' \
-d '{
"uid":"13579",
"size":2,
"scene_id":"home_feed",
"item_list":[{"item_id":"1111", "score":1},{"item_id":"222", "score":0.95}]
}'Response data
{
"code":200,
"msg":"success",
"request_id":"e332fe9c-7d99-45a8-a047-bc7ec33d07f6",
"size":2,
"experiment_id":"",
"items":[
{
"item_id":"1111",
"score":0.9991594902203332,
"retrieve_id":"ContextItemRecall"
},
...
]
}Call the API using the PAI-Rec diagnostic tool
If you deployed the service on the Service Management page in the PAI-Rec console, you can use the diagnostic tool to debug the service, as shown in the following figure.

Select the service and environment. For Routing Information, select /api/rec/feed.
Item Table: If you have an item details table, you can select it here to associate the item details with the returned data.
Request Data: The request parameters from your recent requests are displayed. You can also enter them manually.
As shown in the following figure, select and enter the relevant parameters, and then click Diagnose to obtain the recommendation results.
