Skip to content

Commit ad496f9

Browse files
committed
feat(backend-core): Update organization metadata
Added a new method in the organizations API to update and merge organization metadata. The method name is updateOrganizationMetadata. It triggers a PATCH /v1/organizations/:id/metadata request to the Clerk API and accepts publicMetadata and privateMetadata parameters. The metadata will be merged with any existing metadata on the organization.
1 parent 632b364 commit ad496f9

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

packages/backend-core/API.md

+19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Reference of the methods supported in the Clerk Backend API wrapper. [API refere
2020
- [revokeInvitation(invitationId)](#revokeinvitationinvitationId)
2121
- [Organization operations](#organization-operations)
2222
- [createOrganization(params)](#createorganizationparams)
23+
- [updateOrganizationMetadata(params)](#updateorganizationmetadataparams)
2324
- [Session operations](#session-operations)
2425
- [getSessionList({ clientId, userId })](#getsessionlist-clientid-userid-)
2526
- [getSession(sessionId)](#getsessionsessionid)
@@ -170,6 +171,24 @@ const organization = await clerkAPI.organizations.createOrganization({
170171
});
171172
```
172173

174+
#### updateOrganizationMetadata(params)
175+
176+
Update an organization's metadata attributes by merging existing values with the provided parameters.
177+
178+
You can remove metadata keys at any level by setting their value to `null`.
179+
180+
Available parameters are:
181+
182+
- _publicMetadata_ Metadata saved on the organization, that is visible to both your Frontend and Backend APIs.
183+
- _privateMetadata_ Metadata saved on the organization, that is only visible to your Backend API.
184+
185+
```js
186+
const organization = await clerkAPI.organizations.updateOrganizationMetadata({
187+
publicMetadata: { color: 'blue' },
188+
privateMetadata: { sandbox_mode: true },
189+
});
190+
```
191+
173192
## Session operations
174193

175194
Session operations are exposed by the `sessions` sub-api (`clerkAPI.sessions`).

packages/backend-core/src/__tests__/apis/OrganizationApi.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,40 @@ test('createOrganization() creates an organization', async () => {
4949
}),
5050
);
5151
});
52+
53+
test('updateOrganizationMetadata() updates organization metadata', async () => {
54+
const id = 'org_randomid';
55+
const publicMetadata = { hello: 'world' };
56+
const privateMetadata = { goodbye: 'world' };
57+
const resJSON = {
58+
object: 'organization',
59+
id,
60+
name: 'Org',
61+
public_metadata: publicMetadata,
62+
private_metadata: privateMetadata,
63+
created_at: 1611948436,
64+
updated_at: 1611948436,
65+
};
66+
67+
nock('https://api.clerk.dev')
68+
.patch(`/v1/organizations/${id}/metadata`, {
69+
public_metadata: JSON.stringify(publicMetadata),
70+
private_metadata: JSON.stringify(privateMetadata),
71+
})
72+
.reply(200, resJSON);
73+
74+
const organization = await TestBackendAPIClient.organizations.updateOrganizationMetadata(id, {
75+
publicMetadata,
76+
privateMetadata,
77+
});
78+
expect(organization).toEqual(
79+
new Organization({
80+
id,
81+
name: resJSON.name,
82+
publicMetadata,
83+
privateMetadata,
84+
createdAt: resJSON.created_at,
85+
updatedAt: resJSON.updated_at,
86+
}),
87+
);
88+
});

packages/backend-core/src/api/collection/OrganizationApi.ts

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type OrganizationMetadataRequestBody = {
1919
privateMetadata?: string;
2020
};
2121

22+
type UpdateMetadataParams = OrganizationMetadataParams;
23+
2224
export class OrganizationApi extends AbstractApi {
2325
public async createOrganization(params: CreateParams) {
2426
const { publicMetadata, privateMetadata } = params;
@@ -34,6 +36,16 @@ export class OrganizationApi extends AbstractApi {
3436
},
3537
});
3638
}
39+
40+
public async updateOrganizationMetadata(organizationId: string, params: UpdateMetadataParams) {
41+
this.requireId(organizationId);
42+
43+
return this._restClient.makeRequest<Organization>({
44+
method: 'PATCH',
45+
path: `${basePath}/${organizationId}/metadata`,
46+
bodyParams: stringifyMetadataParams(params),
47+
});
48+
}
3749
}
3850

3951
function stringifyMetadataParams(

0 commit comments

Comments
 (0)