主题
on_page/lighthouse/audits
GET /v3/on_page/lighthouse/audits
接口说明
GET /v3/on_page/lighthouse/audits
本接口基于开源 Lighthouse 项目,用于获取当前可用的 Lighthouse 审计项名称列表。Lighthouse 审计项是对网页质量进行检测后生成的结果对象。
获取审计项名称后,可将对应名称传 Lighthouse 任务创建接口,以便在任务中请求指定审计项的检测结果。
计费说明
当前使用本接口不收取费用,参考价为 ¥0 / 次。
请求示例
cURL
bash
curl --location --request GET \
"https://api.seermartech.cn/v3/on_page/lighthouse/audits" \
--header "Authorization: Bearer smt_live_YOUR_KEY" \
--header "Content-Type: application/json"PHP
php
<?php
require('RestClient.php');
$apiUrl = 'https://api.seermartech.cn/';
try {
// 使用本平台 API Key 进行认证
$client = new RestClient(
$apiUrl,
null,
'Bearer smt_live_YOUR_KEY'
);
} catch (RestClientException $e) {
echo "\n";
print "HTTP code: {$e->getHttpCode()}\n";
print "Error code: {$e->getCode()}\n";
print "Message: {$e->getMessage()}\n";
print $e->getTraceAsString();
echo "\n";
exit();
}
try {
// 获取可用的 Lighthouse 审计项列表
// GET /v3/on_page/lighthouse/audits
$result = $client->get('/v3/on_page/lighthouse/audits');
print_r($result);
} catch (RestClientException $e) {
echo "\n";
print "HTTP code: {$e->getHttpCode()}\n";
print "Error code: {$e->getCode()}\n";
print "Message: {$e->getMessage()}\n";
print $e->getTraceAsString();
echo "\n";
}
$client = null;
?>> 如果所使用的 PHP 客户端支持标准请求头,请将认证信息设置为: > > Authorization: Bearer smt_live_YOUR_KEY
JavaScript / TypeScript
typescript
import axios from 'axios';
axios({
method: 'get',
url: 'https://api.seermartech.cn/v3/on_page/lighthouse/audits',
headers: {
Authorization: 'Bearer smt_live_YOUR_KEY',
'Content-Type': 'application/json',
},
})
.then((response) => {
// 响应数据
console.log(response.data);
})
.catch((error) => {
console.error(error.response?.data || error.message);
});Python
python
import requests
url = "https://api.seermartech.cn/v3/on_page/lighthouse/audits"
headers = {
"Authorization": "Bearer smt_live_YOUR_KEY",
"Content-Type": "application/json",
}
response = requests.get(url, headers=headers)
data = response.json()
if data.get("status_code") == 20000:
print(data)
# 在此处理审计项列表
else:
print(
"请求失败。状态码: %s,信息: %s"
% (data.get("status_code"), data.get("status_message"))
)C#
csharp
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace SeerMarTechDemos
{
public static class Demos
{
public static async Task GetLighthouseAudits()
{
using var httpClient = new HttpClient
{
BaseAddress = new Uri("https://api.seermartech.cn/")
};
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer",
"smt_live_YOUR_KEY"
);
// 获取可用的 Lighthouse 审计项列表
var response = await httpClient.GetAsync(
"/v3/on_page/lighthouse/audits"
);
var content = await response.Content.ReadAsStringAsync();
var result = JObject.Parse(content);
if ((int?)result["status_code"] == 20000)
{
Console.WriteLine(result);
}
else
{
Console.WriteLine(
$"请求失败。状态码: {result["status_code"]}," +
$"信息: {result["status_message"]}"
);
}
}
}
}响应字段
接口返回一个任务执行信息和审计项列表的结果对象。
顶层字段
| 字段 | 类型 | 说明 |
|---|---|---|
version | string | 当前 API 版本 |
status_code | integer | 通用状态码。20000 表示请求成功;完整状态码列表请参考错误码文档。 |
status_message | string | 通用状态信息 |
time | string | 接口执行耗时,单位为秒 |
cost | float | 平台原始 USD 成本兼容字段;人民币实扣以 X-SeerMarTech-Charge-CNY 为准。 |
tasks_count | integer | tasks 数组中的任务数量 |
tasks_error | integer | tasks 数组中返回错误的任务数量 |
tasks | array | 任务对象数组 |
tasks 字段
| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 任务唯一标识,采用 UUID 格式 |
status_code | integer | 任务状态码,通常位于 10000 至 60000 范围 |
status_message | string | 任务状态信息 |
time | string | 任务执行耗时,单位为秒 |
cost | float | 平台原始 USD 成本兼容字段;人民币实扣以 X-SeerMarTech-Charge-CNY 为准。 |
result_count | integer | result 数组中的数量 |
path | array | 请求 URL 路径 |
data | object | 请求参数。对于本 GET 接口,通常接口信息。 |
result | array | 任务结果数组 |
result 字段
| 字段 | 类型 | 说明 |
|---|---|---|
audits | array | 当前可用的 Lighthouse 审计项名称列表 |
audits 数组中的每个代表一个可用的审计项标题。
> 审计项标题可能会随着审计通过或失败而变化,也可能 Markdown 标记。 > > 如果审计项名称中斜杠 /,建议使用斜杠后的最后一个词进行匹或检索。
响应示例
json
{
"version": "0.1.20210713",
"status_code": 20000,
"status_message": "Ok.",
"time": "0.3788 sec.",
"cost": 0,
"tasks_count": 1,
"tasks_error": 0,
"tasks": [
{
"id": "00000000-0000-0000-0000-000000000000",
"status_code": 20000,
"status_message": "Ok.",
"time": "0.1000 sec.",
"cost": 0,
"result_count": 1,
"path": [
"v3",
"on_page",
"lighthouse",
"audits"
],
"data": {
"api": "on_page",
"function": "lighthouse"
},
"result": [
{
"audits": [
"first-contentful-paint",
"largest-contentful-paint",
"interactive",
"speed-index",
"total-blocking-time",
"cumulative-layout-shift"
]
}
]
}
]
}错误处理
建议在业务系统中同时检查以下字段:
- 顶层
status_code - 任务级
tasks[].status_code - 顶层及任务级
status_message
当状态码不是 20000 时,应根据返回的状态信息进行错误记录、重试或人工排查。
实用场景
- 获取可用审计项名称:在创建 Lighthouse 检测任务前动态读取审计项,因审计项名称变化导致任务参数失效。
- 构建审计项选择器:将返回的审计项列表展示在 SEO 技术审计后台,支持用户按需选择检测指标。
- 同步 Lighthouse 指标:定期更新系统中的审计项字,确保前端性能监控和技术 SEO 报告使用最新指标。
- 匹指定性能指标:根据审计项名称筛选最大绘制、累计布局偏移和总阻塞时间等核心网页性能指标。
- 校验任务请求参数:在提交 Lighthouse 任务前检查目标审计项是否存在,减少无效任务和错误响应。