Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(api): add `DELETE /api/public/score/[scoreId]` (#1167)

Cheng Kuan Yong Jason5e8ec0bd13a4
diff --git a/fern/apis/server/definition/score.yml b/fern/apis/server/definition/score.yml
index d3fab1eb8e..7dc41cf729 100644
--- a/fern/apis/server/definition/score.yml
+++ b/fern/apis/server/definition/score.yml
@@ -24,6 +24,14 @@ service:
userId: optional<string>
name: optional<string>
response: Scores
+ delete:
+ docs: Delete a score
+ method: DELETE
+ path: /scores/{scoreId}
+ path-parameters:
+ scoreId:
+ type: string
+ docs: The unique langfuse identifier of a score
types:
CreateScoreRequest:
properties:
diff --git a/generated/openapi-server/openapi.yml b/generated/openapi-server/openapi.yml
index 5c30910c5f..326c29286d 100644
--- a/generated/openapi-server/openapi.yml
+++ b/generated/openapi-server/openapi.yml
@@ -761,6 +761,48 @@ paths:
application/json:
schema: {}
security: *ref_0
+ /api/public/scores/{scoreId}:
+ delete:
+ description: Delete a score
+ operationId: score_delete
+ tags:
+ - Score
+ parameters:
+ - name: scoreId
+ in: path
+ description: The unique langfuse identifier of a score
+ required: true
+ schema:
+ type: string
+ responses:
+ '204':
+ description: ''
+ '400':
+ description: ''
+ content:
+ application/json:
+ schema: {}
+ '401':
+ description: ''
+ content:
+ application/json:
+ schema: {}
+ '403':
+ description: ''
+ content:
+ application/json:
+ schema: {}
+ '404':
+ description: ''
+ content:
+ application/json:
+ schema: {}
+ '405':
+ description: ''
+ content:
+ application/json:
+ schema: {}
+ security: *ref_0
/api/public/sessions/{sessionId}:
get:
description: Get a session
diff --git a/generated/postman/collection.json b/generated/postman/collection.json
index dd382b5d96..3f65a83b1a 100644
--- a/generated/postman/collection.json
+++ b/generated/postman/collection.json
@@ -623,6 +623,38 @@
"body": null
},
"response": []
+ },
+ {
+ "_type": "endpoint",
+ "name": "Delete",
+ "request": {
+ "description": "Delete a score",
+ "url": {
+ "raw": "{{baseUrl}}/api/public/scores/:scoreId",
+ "host": [
+ "{{baseUrl}}"
+ ],
+ "path": [
+ "api",
+ "public",
+ "scores",
+ ":scoreId"
+ ],
+ "query": [],
+ "variable": [
+ {
+ "key": "scoreId",
+ "value": "",
+ "description": "The unique langfuse identifier of a score"
+ }
+ ]
+ },
+ "header": [],
+ "method": "DELETE",
+ "auth": null,
+ "body": null
+ },
+ "response": []
}
]
},
diff --git a/src/__tests__/scores.servertest.ts b/src/__tests__/scores.servertest.ts
index b7452df104..e717e8de60 100644
--- a/src/__tests__/scores.servertest.ts
+++ b/src/__tests__/scores.servertest.ts
@@ -231,4 +231,43 @@ describe("/api/public/scores API Endpoint", () => {
expect(dbScore?.comment).toBe("comment-updated");
expect(dbScore?.observationId).toBe(dbGeneration[0]!.id);
});
+
+ it("should delete a score", async () => {
+ await pruneDatabase();
+
+ const traceId = uuidv4();
+
+ await makeAPICall("POST", "/api/public/traces", {
+ id: traceId,
+ });
+
+ const scoreId = uuidv4();
+ const createScore = await makeAPICall("POST", "/api/public/scores", {
+ id: scoreId,
+ name: "score-name",
+ value: 100.5,
+ traceId: traceId,
+ comment: "comment",
+ });
+
+ expect(createScore.status).toBe(200);
+ const dbScore = await prisma.score.findUnique({
+ where: {
+ id: scoreId,
+ },
+ });
+ expect(dbScore?.id).toBe(scoreId);
+
+ const deleteScore = await makeAPICall(
+ "DELETE",
+ `/api/public/scores/${scoreId}`,
+ );
+ expect(deleteScore.status).toBe(200);
+ const deletedScore = await prisma.score.findUnique({
+ where: {
+ id: scoreId,
+ },
+ });
+ expect(deletedScore).toBeNull();
+ });
});
diff --git a/src/pages/api/public/scores/[scoreId].ts b/src/pages/api/public/scores/[scoreId].ts
new file mode 100644
index 0000000000..2098c1fa70
--- /dev/null
+++ b/src/pages/api/public/scores/[scoreId].ts
@@ -0,0 +1,78 @@
+import { prisma } from "@/src/server/db";
+import { type NextApiRequest, type NextApiResponse } from "next";
+import { z } from "zod";
+import { cors, runMiddleware } from "@/src/features/public-api/server/cors";
+import { verifyAuthHeaderAndReturnScope } from "@/src/features/public-api/server/apiAuth";
+
+const ScoreDeleteSchema = z.object({
+ scoreId: z.string(),
+});
+
+export default async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ await runMiddleware(req, res, cors);
+
+ // CHECK AUTH
+ const authCheck = await verifyAuthHeaderAndReturnScope(
+ req.headers.authorization,
+ );
+ if (!authCheck.validKey)
+ return res.status(401).json({
+ message: authCheck.error,
+ });
+ // END CHECK AUTH
+
+ if (req.method === "DELETE") {
+ try {
+ if (authCheck.scope.accessLevel !== "all") {
+ return res.status(401).json({
+ message:
+ "Access denied - need to use basic auth with secret key to DELETE scores",
+ });
+ }
+
+ const { scoreId } = ScoreDeleteSchema.parse(req.query); // uses query and not body
+
+ const score = await prisma.score.findUnique({
+ select: {
+ id: true,
+ },
+ where: {
+ id: scoreId,
+ trace: {
+ projectId: authCheck.scope.projectId,
+ },
+ },
+ });
+
+ if (!score) {
+ return res.status(404).json({
+ message: "Score not found within authorized project",
+ });
+ }
+
+ await prisma.score.delete({
+ where: {
+ id: scoreId,
+ trace: {
+ projectId: authCheck.scope.projectId,
+ },
+ },
+ });
+
+ return res.status(200).json({ message: "Score deleted successfully" });
+ } catch (error: unknown) {
+ console.error(error);
+ const errorMessage =
+ error instanceof Error ? error.message : "An unknown error occurred";
+ res.status(400).json({
+ message: "Invalid request data",
+ error: errorMessage,
+ });
+ }
+ } else {
+ return res.status(405).json({ message: "Method not allowed" });
+ }
+}