Skip to content

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

feat: add membership deletion API endpoints (#8553)

Steffen Schmitz6f06a3ab5f0d
diff --git a/fern/apis/server/definition/organizations.yml b/fern/apis/server/definition/organizations.yml
index 1a3fd3ccab..49c18bdb51 100644
--- a/fern/apis/server/definition/organizations.yml
+++ b/fern/apis/server/definition/organizations.yml
@@ -20,6 +20,13 @@ service:
request: MembershipRequest
response: MembershipResponse
+ deleteOrganizationMembership:
+ docs: Delete a membership from the organization associated with the API key (requires organization-scoped API key)
+ method: DELETE
+ path: /organizations/memberships
+ request: DeleteMembershipRequest
+ response: MembershipDeletionResponse
+
getProjectMemberships:
docs: Get all memberships for a specific project (requires organization-scoped API key)
method: GET
@@ -37,6 +44,15 @@ service:
request: MembershipRequest
response: MembershipResponse
+ deleteProjectMembership:
+ docs: Delete a membership from a specific project (requires organization-scoped API key). The user must be a member of the organization.
+ method: DELETE
+ path: /projects/{projectId}/memberships
+ path-parameters:
+ projectId: string
+ request: DeleteMembershipRequest
+ response: MembershipDeletionResponse
+
getOrganizationProjects:
docs: Get all projects for the organization associated with the API key (requires organization-scoped API key)
method: GET
@@ -56,6 +72,10 @@ types:
userId: string
role: MembershipRole
+ DeleteMembershipRequest:
+ properties:
+ userId: string
+
MembershipResponse:
properties:
userId: string
@@ -63,6 +83,11 @@ types:
email: string
name: string
+ MembershipDeletionResponse:
+ properties:
+ message: string
+ userId: string
+
MembershipsResponse:
properties:
memberships: list<MembershipResponse>
diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.yml
index 4b708c5b59..ef9c6a5baf 100644
--- a/web/public/generated/api/openapi.yml
+++ b/web/public/generated/api/openapi.yml
@@ -2374,6 +2374,54 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/MembershipRequest'
+ delete:
+ description: >-
+ Delete a membership from the organization associated with the API key
+ (requires organization-scoped API key)
+ operationId: organizations_deleteOrganizationMembership
+ tags:
+ - Organizations
+ parameters: []
+ responses:
+ '200':
+ description: ''
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/MembershipDeletionResponse'
+ '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:
+ - BasicAuth: []
+ requestBody:
+ required: true
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/DeleteMembershipRequest'
/api/public/projects/{projectId}/memberships:
get:
description: >-
@@ -2476,6 +2524,60 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/MembershipRequest'
+ delete:
+ description: >-
+ Delete a membership from a specific project (requires
+ organization-scoped API key). The user must be a member of the
+ organization.
+ operationId: organizations_deleteProjectMembership
+ tags:
+ - Organizations
+ parameters:
+ - name: projectId
+ in: path
+ required: true
+ schema:
+ type: string
+ responses:
+ '200':
+ description: ''
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/MembershipDeletionResponse'
+ '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:
+ - BasicAuth: []
+ requestBody:
+ required: true
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/DeleteMembershipRequest'
/api/public/organizations/projects:
get:
description: >-
@@ -6914,6 +7016,14 @@ components:
required:
- userId
- role
+ DeleteMembershipRequest:
+ title: DeleteMembershipRequest
+ type: object
+ properties:
+ userId:
+ type: string
+ required:
+ - userId
MembershipResponse:
title: MembershipResponse
type: object
@@ -6931,6 +7041,17 @@ components:
- role
- email
- name
+ MembershipDeletionResponse:
+ title: MembershipDeletionResponse
+ type: object
+ properties:
+ message:
+ type: string
+ userId:
+ type: string
+ required:
+ - message
+ - userId
MembershipsResponse:
title: MembershipsResponse
type: object
diff --git a/web/public/generated/postman/collection.json b/web/public/generated/postman/collection.json
index 8328e00091..142a365f25 100644
--- a/web/public/generated/postman/collection.json
+++ b/web/public/generated/postman/collection.json
@@ -1555,6 +1555,40 @@
},
"response": []
},
+ {
+ "_type": "endpoint",
+ "name": "Delete Organization Membership",
+ "request": {
+ "description": "Delete a membership from the organization associated with the API key (requires organization-scoped API key)",
+ "url": {
+ "raw": "{{baseUrl}}/api/public/organizations/memberships",
+ "host": [
+ "{{baseUrl}}"
+ ],
+ "path": [
+ "api",
+ "public",
+ "organizations",
+ "memberships"
+ ],
+ "query": [],
+ "variable": []
+ },
+ "header": [],
+ "method": "DELETE",
+ "auth": null,
+ "body": {
+ "mode": "raw",
+ "raw": "{\n \"userId\": \"example\"\n}",
+ "options": {
+ "raw": {
+ "language": "json"
+ }
+ }
+ }
+ },
+ "response": []
+ },
{
"_type": "endpoint",
"name": "Get Project Memberships",
@@ -1625,6 +1659,47 @@
},
"response": []
},
+ {
+ "_type": "endpoint",
+ "name": "Delete Project Membership",
+ "request": {
+ "description": "Delete a membership from a specific project (requires organization-scoped API key). The user must be a member of the organization.",
+ "url": {
+ "raw": "{{baseUrl}}/api/public/projects/:projectId/memberships",
+ "host": [
+ "{{baseUrl}}"
+ ],
+ "path": [
+ "api",
+ "public",
+ "projects",
+ ":projectId",
+ "memberships"
+ ],
+ "query": [],
+ "variable": [
+ {
+ "key": "projectId",
+ "value": "",
+ "description": null
+ }
+ ]
+ },
+ "header": [],
+ "method": "DELETE",
+ "auth": null,
+ "body": {
+ "mode": "raw",
+ "raw": "{\n \"userId\": \"example\"\n}",
+ "options": {
+ "raw": {
+ "language": "json"
+ }
+ }
+ }
+ },
+ "response": []
+ },
{
"_type": "endpoint",
"name": "Get Organization Projects",
diff --git a/web/src/__tests__/async/memberships-api.servertest.ts b/web/src/__tests__/async/memberships-api.servertest.ts
index d12d89c275..e370cf98ef 100644
--- a/web/src/__tests__/async/memberships-api.servertest.ts
+++ b/web/src/__tests__/async/memberships-api.servertest.ts
@@ -281,6 +281,81 @@ describe("Memberships APIs", () => {
});
});
});
+
+ describe("DELETE /api/public/projects/[projectId]/memberships", () => {
+ it("should delete an existing project membership with valid API key", async () => {
+ // First ensure the user has an organization membership
+ const orgMembership = await prisma.organizationMembership.upsert({
+ where: {
+ orgId_userId: {
+ userId: testUserId,
+ orgId: testOrgId,
+ },
+ },
+ update: {},
+ create: {
+ userId: testUserId,
+ orgId: testOrgId,
+ role: Role.MEMBER,
+ },
+ });
+
+ // Create a project membership
+ await prisma.projectMembership.upsert({
+ where: {
+ projectId_userId: {
+ userId: testUserId,
+ projectId: testProjectId,
+ },
+ },
+ update: {},
+ create: {
+ userId: testUserId,
+ projectId: testProjectId,
+ role: Role.VIEWER,
+ orgMembershipId: orgMembership.id,
+ },
+ });
+
+ // Verify membership exists before deletion
+ const membershipBefore = await prisma.projectMembership.findUnique({
+ where: {
+ projectId_userId: {
+ userId: testUserId,
+ projectId: testProjectId,
+ },
+ },
+ });
+ expect(membershipBefore).not.toBeNull();
+
+ // Delete the membership
+ const response = await makeAPICall(
+ "DELETE",
+ `/api/public/projects/${testProjectId}/memberships`,
+ {
+ userId: testUserId,
+ },
+ createBasicAuthHeader(testApiKey, testApiSecretKey),
+ );
+
+ expect(response.status).toBe(200);
+ expect(response.body.message).toBe(
+ "Project membership deleted successfully",
+ );
+ expect(response.body.userId).toBe(testUserId);
+
+ // Verify the membership was deleted from the database
+ const membershipAfter = await prisma.projectMembership.findUnique({
+ where: {
+ projectId_userId: {
+ userId: testUserId,
+ projectId: testProjectId,
+ },
+ },
+ });
+ expect(membershipAfter).toBeNull();
+ });
+ });
});
describe("Organization Memberships", () => {
@@ -404,5 +479,63 @@ describe("Memberships APIs", () => {
expect(result.status).toBe(404);
});
});
+
+ describe("DELETE /api/public/organizations/memberships", () => {
+ it("should delete an existing organization membership with valid API key", async () => {
+ // First ensure the membership exists
+ await prisma.organizationMembership.upsert({
+ where: {
+ orgId_userId: {
+ userId: testUserId,
+ orgId: testOrgId,
+ },
+ },
+ update: {},
+ create: {
+ userId: testUserId,
+ orgId: testOrgId,
+ role: Role.MEMBER,
+ },
+ });
+
+ // Verify membership exists before deletion
+ const membershipBefore = await prisma.organizationMembership.findUnique(
+ {
+ where: {
+ orgId_userId: {
+ userId: testUserId,
+ orgId: testOrgId,
+ },
+ },
+ },
+ );
+ expect(membershipBefore).not.toBeNull();
+
+ // Delete the membership
+ const response = await makeAPICall(
+ "DELETE",
+ `/api/public/organizations/memberships`,
+ {
+ userId: testUserId,
+ },
+ createBasicAuthHeader(testApiKey, testApiSecretKey),
+ );
+
+ expect(response.status).toBe(200);
+ expect(response.body.message).toBe("Membership deleted successfully");
+ expect(response.body.userId).toBe(testUserId);
+
+ // Verify the membership was deleted from the database
+ const membershipAfter = await prisma.organizationMembership.findUnique({
+ where: {
+ orgId_userId: {
+ userId: testUserId,
+ orgId: testOrgId,
+ },
+ },
+ });
+ expect(membershipAfter).toBeNull();
+ });
+ });
});
});
diff --git a/web/src/ee/features/admin-api/server/memberships.ts b/web/src/ee/features/admin-api/server/memberships.ts
index c3ad5353a7..f6db06b886 100644
--- a/web/src/ee/features/admin-api/server/memberships.ts
+++ b/web/src/ee/features/admin-api/server/memberships.ts
@@ -9,6 +9,11 @@ const MembershipSchema = z.object({
role: z.enum(Role),
});
+// Schema for delete request body validation
+const DeleteMembershipSchema = z.object({
+ userId: z.string(),
+});
+
// GET - Retrieve all organization memberships
export async function handleGetMemberships(
req: NextApiRequest,
@@ -92,3 +97,31 @@ export async function handleUpdateMembership(
name: user.name,
});
}
+
+// DELETE - Remove an organization membership
+export async function handleDeleteMembership(
+ req: NextApiRequest,
+ res: NextApiResponse,
+ orgId: string,
+) {
+ const validatedBody = DeleteMembershipSchema.safeParse(req.body);
+ if (!validatedBody.success) {
+ return res.status(400).json({
+ error: "Invalid request body",
+ details: validatedBody.error.issues,
+ });
+ }
+
+ // Delete the membership (using deleteMany to avoid errors on not found)
+ await prisma.organizationMembership.deleteMany({
+ where: {
+ orgId,
+ userId: validatedBody.data.userId,
+ },
+ });
+
+ return res.status(200).json({
+ message: "Membership deleted successfully",
+ userId: validatedBody.data.userId,
+ });
+}
diff --git a/web/src/ee/features/admin-api/server/projects/projectById/memberships/index.ts b/web/src/ee/features/admin-api/server/projects/projectById/memberships/index.ts
index 60db793049..d95a6ac17a 100644
--- a/web/src/ee/features/admin-api/server/projects/projectById/memberships/index.ts
+++ b/web/src/ee/features/admin-api/server/projects/projectById/memberships/index.ts
@@ -9,6 +9,11 @@ const MembershipSchema = z.object({
role: z.enum(Role),
});
+// Schema for delete request body validation
+const DeleteMembershipSchema = z.object({
+ userId: z.string(),
+});
+
// GET - Retrieve all project memberships
export async function handleGetMemberships(
req: NextApiRequest,
@@ -109,3 +114,64 @@ export async function handleUpdateMembership(
name: orgMembership.user.name,
});
}
+
+// DELETE - Remove a project membership
+export async function handleDeleteMembership(
+ req: NextApiRequest,
+ res: NextApiResponse,
+ projectId: string,
+ orgId: string,
+) {
+ const validatedBody = DeleteMembershipSchema.safeParse(req.body);
+ if (!validatedBody.success) {
+ return res.status(400).json({
+ error: "Invalid request body",
+ details: validatedBody.error.issues,
+ });
+ }
+
+ // Check if membership exists
+ const membership = await prisma.projectMembership.findUnique({
+ where: {
+ projectId_userId: {
+ userId: validatedBody.data.userId,
+ projectId: projectId,
+ },
+ },
+ include: {
+ organizationMembership: {
+ select: {
+ orgId: true,
+ },
+ },
+ },
+ });
+
+ if (!membership) {
+ return res.status(404).json({
+ error: "Project membership not found",
+ });
+ }
+
+ // Verify the membership belongs to the correct organization
+ if (membership.organizationMembership.orgId !== orgId) {
+ return res.status(403).json({
+ error: "Project membership does not belong to this organization",
+ });
+ }
+
+ // Delete the membership
+ await prisma.projectMembership.delete({
+ where: {
+ projectId_userId: {
+ userId: validatedBody.data.userId,
+ projectId: projectId,
+ },
+ },
+ });
+
+ return res.status(200).json({
+ message: "Project membership deleted successfully",
+ userId: validatedBody.data.userId,
+ });
+}
diff --git a/web/src/pages/api/public/organizations/memberships/index.ts b/web/src/pages/api/public/organizations/memberships/index.ts
index bd7d0b3e2a..ce3f6ac9c1 100644
--- a/web/src/pages/api/public/organizations/memberships/index.ts
+++ b/web/src/pages/api/public/organizations/memberships/index.ts
@@ -5,6 +5,7 @@ import { logger, redis } from "@langfuse/shared/src/server";
import {
handleGetMemberships,
handleUpdateMembership,
+ handleDeleteMembership,
} from "@/src/ee/features/admin-api/server/memberships";
import { type NextApiRequest, type NextApiResponse } from "next";
@@ -16,7 +17,7 @@ export default async function handler(
) {
await runMiddleware(req, res, cors);
- if (!["GET", "PUT"].includes(req.method || "")) {
+ if (!["GET", "PUT", "DELETE"].includes(req.method || "")) {
logger.error(
`Method not allowed for ${req.method} on /api/public/organizations/memberships`,
);
@@ -66,6 +67,8 @@ export default async function handler(
return handleGetMemberships(req, res, authCheck.scope.orgId);
case "PUT":
return handleUpdateMembership(req, res, authCheck.scope.orgId);
+ case "DELETE":
+ return handleDeleteMembership(req, res, authCheck.scope.orgId);
default:
// This should never happen due to the check at the beginning
return res.status(405).json({
diff --git a/web/src/pages/api/public/projects/[projectId]/memberships/index.ts b/web/src/pages/api/public/projects/[projectId]/memberships/index.ts
index fe52103d31..170c2129fe 100644
--- a/web/src/pages/api/public/projects/[projectId]/memberships/index.ts
+++ b/web/src/pages/api/public/projects/[projectId]/memberships/index.ts
@@ -6,6 +6,7 @@ import { hasEntitlementBasedOnPlan } from "@/src/features/entitlements/server/ha
import {
handleGetMemberships,
handleUpdateMembership,
+ handleDeleteMembership,
} from "@/src/ee/features/admin-api/server/projects/projectById/memberships";
import { type NextApiRequest, type NextApiResponse } from "next";
@@ -16,7 +17,7 @@ export default async function handler(
) {
await runMiddleware(req, res, cors);
- if (!["GET", "PUT"].includes(req.method || "")) {
+ if (!["GET", "PUT", "DELETE"].includes(req.method || "")) {
logger.error(
`Method not allowed for ${req.method} on /api/public/projects/[projectId]/memberships`,
);
@@ -106,6 +107,13 @@ export default async function handler(
projectId,
authCheck.scope.orgId,
);
+ case "DELETE":
+ return handleDeleteMembership(
+ req,
+ res,
+ projectId,
+ authCheck.scope.orgId,
+ );
default:
// This should never happen due to the check at the beginning
return res.status(405).json({