Organization domain methods
These methods on the Organization
object allow you to manage the domains of an organization.
The following examples assume:
- you have followed the quickstart in order to add Clerk to your JavaScript application
- you have enabled the Organizations feature in your Clerk Dashboard
- you have enabled Verified domains for your organization
createDomain()
Creates a new domain for the currently active organization.
function createDomain: (domainName: string) => Promise<OrganizationDomainResource>;
createDomain()
parameters
Name | Type | Description |
---|---|---|
domainName | string | The domain name that will be added to the organization. |
createDomain()
returns
Type | Description |
---|---|
Promise<OrganizationDomain> | This method returns a Promise that resolves to the OrganizationDomain for the corresponding ID. |
createDomain()
example
main.jsimport Clerk from '@clerk/clerk-js'; // Initialize Clerk with your Clerk publishable key const clerk = new Clerk('{{pub_key}}'); await clerk.load(); if (clerk.user) { // Check for an active organization if (clerk.organization) { await clerk.organization.createDomain("test-domain.com") .then((res) => console.log(res)) .catch((error) => console.log("An error occurred:", error.errors)); } else { // If there is no active organization, // mount Clerk's <OrganizationSwitcher /> // to allow the user to set an organization as active document.getElementById("app").innerHTML = ` <h2>Select an organization to set it as active</h2> <div id="org-switcher"></div> `; const orgSwitcherDiv = document.getElementById("org-switcher"); clerk.mountOrganizationSwitcher(orgSwitcherDiv); } } else { document.getElementById("app").innerHTML = ` <div id="sign-in"></div> `; const signInDiv = document.getElementById("sign-in"); clerk.mountSignIn(signInDiv); }
index.html<div id="app"></div> <!-- Initialize Clerk with your Clerk Publishable key and Frontend API URL --> <script async crossorigin="anonymous" data-clerk-publishable-key="{{pub_key}}" src="https://{{fapi_url}}/npm/@clerk/clerk-js@latest/dist/clerk.browser.js" type="text/javascript" ></script> <script> window.addEventListener("load", async function () { await Clerk.load(); if (Clerk.user) { // Check for an active organization if (Clerk.organization) { await Clerk.organization.createDomain("test-domain.com") .then((res) => console.log(res)) .catch((error) => console.log("An error occurred:", error.errors)); } else { // If there is no active organization, // mount Clerk's <OrganizationSwitcher /> // to allow the user to set an organization as active document.getElementById("app").innerHTML = ` <h2>Select an organization to set it as active</h2> <div id="org-switcher"></div> `; const orgSwitcherDiv = document.getElementById("org-switcher"); Clerk.mountOrganizationSwitcher(orgSwitcherDiv); } } else { document.getElementById("app").innerHTML = ` <div id="sign-in"></div> `; const signInDiv = document.getElementById("sign-in"); Clerk.mountSignIn(signInDiv); } }); </script>
getDomains()
Retrieves the list of domains for the currently active organization.
function getDomains(params?: GetDomainsParams): Promise<ClerkPaginatedResponse<OrganizationDomain>>;
GetDomainsParams
Name | Type | Description |
---|---|---|
initialPage? | number | A number that can be used to skip the first n-1 pages. For example, if initialPage is set to 10, it is will skip the first 9 pages and will fetch the 10th page. |
pageSize? | number | A number that indicates the maximum number of results that should be returned for a specific page. |
enrollmentMode? | 'manual_invitation' | 'automatic_invitation' | 'automatic_suggestion' | An enrollment mode will change how new users join an organization. |
getDomains()
returns
Type | Description |
---|---|
Promise<ClerkPaginatedResponse<OrganizationDomain>> | This method returns a Promise that resolves to a ClerkPaginatedResponse of OrganizationDomain objects. |
getDomains()
example
main.jsimport Clerk from '@clerk/clerk-js'; // Initialize Clerk with your Clerk publishable key const clerk = new Clerk('{{pub_key}}'); await clerk.load(); if (clerk.user) { // Check for an active organization if (clerk.organization) { await clerk.organization.getDomains() .then((res) => console.log(res)) .catch((error) => console.log("An error occurred:", error.errors)); } else { // If there is no active organization, // mount Clerk's <OrganizationSwitcher /> // to allow the user to set an organization as active document.getElementById("app").innerHTML = ` <h2>Select an organization to set it as active</h2> <div id="org-switcher"></div> `; const orgSwitcherDiv = document.getElementById("org-switcher"); clerk.mountOrganizationSwitcher(orgSwitcherDiv); } } else { document.getElementById("app").innerHTML = ` <div id="sign-in"></div> `; const signInDiv = document.getElementById("sign-in"); clerk.mountSignIn(signInDiv); }
index.html<div id="app"></div> <!-- Initialize Clerk with your Clerk Publishable key and Frontend API URL --> <script async crossorigin="anonymous" data-clerk-publishable-key="{{pub_key}}" src="https://{{fapi_url}}/npm/@clerk/clerk-js@latest/dist/clerk.browser.js" type="text/javascript" ></script> <script> window.addEventListener("load", async function () { await Clerk.load(); if (Clerk.user) { // Check for an active organization if (Clerk.organization) { await Clerk.organization.getDomains() .then((res) => console.log(res)) .catch((error) => console.log("An error occurred:", error.errors)); } else { // If there is no active organization, // mount Clerk's <OrganizationSwitcher /> // to allow the user to set an organization as active document.getElementById("app").innerHTML = ` <h2>Select an organization to set it as active</h2> <div id="org-switcher"></div> `; const orgSwitcherDiv = document.getElementById("org-switcher"); Clerk.mountOrganizationSwitcher(orgSwitcherDiv); } } else { document.getElementById("app").innerHTML = ` <div id="sign-in"></div> `; const signInDiv = document.getElementById("sign-in"); Clerk.mountSignIn(signInDiv); } }); </script>
getDomain()
Retrieves a domain for an organization based on the given domain ID.
function getDomain(params: GetDomainParams): Promise<OrganizationDomain>;
GetDomainParams
Name | Type | Description |
---|---|---|
domainId | string | The ID of the domain that will be fetched. |
getDomain()
returns
Type | Description |
---|---|
Promise<OrganizationDomain> | This method returns a Promise that resolves to the OrganizationDomain for the corresponding ID. |
getDomain()
example
main.jsimport Clerk from '@clerk/clerk-js'; // Initialize Clerk with your Clerk publishable key const clerk = new Clerk('{{pub_key}}'); await clerk.load(); if (clerk.user) { // Check for an active organization if (clerk.organization) { const domainId = "orgdmn_123"; await clerk.organization.getDomain({ domainId }) .then((res) => console.log(`Domains:`, res)) .catch((error) => console.log("An error occurred:", error.errors)); } else { // If there is no active organization, // mount Clerk's <OrganizationSwitcher /> // to allow the user to set an organization as active document.getElementById("app").innerHTML = ` <h2>Select an organization to set it as active</h2> <div id="org-switcher"></div> `; const orgSwitcherDiv = document.getElementById("org-switcher"); clerk.mountOrganizationSwitcher(orgSwitcherDiv); } } else { document.getElementById("app").innerHTML = ` <div id="sign-in"></div> `; const signInDiv = document.getElementById("sign-in"); clerk.mountSignIn(signInDiv); }
index.html<div id="app"></div> <!-- Initialize Clerk with your Clerk Publishable key and Frontend API URL --> <script async crossorigin="anonymous" data-clerk-publishable-key="{{pub_key}}" src="https://{{fapi_url}}/npm/@clerk/clerk-js@latest/dist/clerk.browser.js" type="text/javascript" ></script> <script> window.addEventListener("load", async function () { await Clerk.load(); if (Clerk.user) { // Check for an active organization if (Clerk.organization) { const domainId = "orgdmn_123"; await Clerk.organization.getDomain({ domainId }) .then((res) => console.log(`Domains:`, res)) .catch((error) => console.log("An error occurred:", error.errors)); } else { // If there is no active organization, // mount Clerk's <OrganizationSwitcher /> // to allow the user to set an organization as active document.getElementById("app").innerHTML = ` <h2>Select an organization to set it as active</h2> <div id="org-switcher"></div> `; const orgSwitcherDiv = document.getElementById("org-switcher"); Clerk.mountOrganizationSwitcher(orgSwitcherDiv); } } else { document.getElementById("app").innerHTML = ` <div id="sign-in"></div> `; const signInDiv = document.getElementById("sign-in"); Clerk.mountSignIn(signInDiv); } }); </script>
Last updated on March 26, 2024