Vanilla JavaScript examples for Auth0 MyOrganization SDK in Node.js (no TypeScript).
- ✅ Pure JavaScript (no TypeScript required)
- ✅ Simple automation scripts
- ✅ All authentication methods
- ✅ Complete error handling
- ✅ Easy to understand and modify
- Node.js 20+
- An Auth0 tenant with the MyOrganization API enabled
- A Machine-to-Machine (M2M) application in Auth0
- Go to Auth0 Dashboard → Applications → APIs
- Find Auth0 My Organization API and confirm it is enabled
- Go to Applications → Applications → Create Application
- Select Machine to Machine Applications
- Authorize it for the Auth0 My Organization API
- Grant the following scopes:
read:my_org:details
update:my_org:details
read:my_org:organization_domains
create:my_org:organization_domains
update:my_org:organization_domains
delete:my_org:organization_domains
read:my_org:identity_providers
create:my_org:identity_providers
update:my_org:identity_providers
delete:my_org:identity_providers
From the M2M application's Settings tab, copy:
- Domain — your Auth0 tenant domain (e.g.
your-tenant.auth0.com) - Client ID
- Client Secret (or configure a private key for private key JWT)
- Go to Auth0 Dashboard → Organizations
- Select your organization
- Copy the Organization ID (starts with
org_)
npm installcp .env.example .envEdit .env with your credentials:
AUTH0_DOMAIN=your-tenant.auth0.com
AUTH0_CLIENT_ID=your-client-id
AUTH0_CLIENT_SECRET=your-client-secret # or use AUTH0_PRIVATE_KEY
AUTH0_ORGANIZATION=org_123456789Run the example:
npm startimport { createMyOrganizationClientWithClientCredentials } from "@auth0/myorganization-js/server";
const client = createMyOrganizationClientWithClientCredentials(
{ domain: "tenant.auth0.com" },
{
clientId: process.env.AUTH0_CLIENT_ID,
clientSecret: process.env.AUTH0_CLIENT_SECRET,
organization: process.env.AUTH0_ORGANIZATION,
},
);const details = await client.organizationDetails.get();
console.log("Organization:", details.name);const result = await client.organization.domains.create({
domain: "example.com",
});
console.log("Domain created:", result.id);try {
await client.organization.domains.create({ domain: "example.com" });
} catch (error) {
if (error.statusCode === 400) {
console.error("Bad request:", error.message);
} else if (error.statusCode === 409) {
console.error("Domain already exists");
} else {
console.error("Error:", error.message);
}
}Use private key JWT in production — more secure than a client secret:
AUTH0_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----"Never commit your .env file. Use a secrets manager (AWS Secrets Manager, Azure Key Vault, HashiCorp Vault, etc.) for production deployments.
Missing required environment variables — Check that AUTH0_DOMAIN, AUTH0_CLIENT_ID, AUTH0_ORGANIZATION, and either AUTH0_CLIENT_SECRET or AUTH0_PRIVATE_KEY are all set in .env.
401 Unauthorized — Verify your client credentials and that the M2M application is authorized for the MyOrganization API.
403 Forbidden — Grant the required scopes listed above to your M2M application in the Auth0 Dashboard.