Paid Feature
This is a paid feature. For self hosted users, Sign up to get a license key and follow the instructions sent to you by email. This will start a SuperTokens subscription.
If you are using the development environment of our managed service you can use this feature for free. If you want to enable this feature for the production environment, please email us
Creating a new tenant
#
Basic tenant creationA new tenant can be created for an app using our backend SDK functions or a cURL command as shown below. If an appId is not explicitly specified, the tenant will be created in the "public"
appId (the default app that is created when the SuperTokens core first starts).
- NodeJS
- GoLang
- Python
- cURL
Important
import Multitenancy from "supertokens-node/recipe/multitenancy";
async function createNewTenant() {
let resp = await Multitenancy.createOrUpdateTenant("customer1", {
emailPasswordEnabled: true,
thirdPartyEnabled: true,
passwordlessEnabled: true
});
if (resp.createdNew) {
// Tenant created successfully
} else {
// Existing tenant's config was modified.
}
}
- In the above, we are creating a new tenant with the id
"customer1"
. We are also enabling the email password, third party and passwordless login for this tenant. You can also disable any of these by setting the corresponding field tofalse
(which is also the default setting).
import (
"github.com/supertokens/supertokens-golang/recipe/multitenancy"
"github.com/supertokens/supertokens-golang/recipe/multitenancy/multitenancymodels"
)
func main() {
tenantId := "customer1"
emailPasswordEnabled := true
thirdPartyEnabled := true
passwordlessEnabled := true
resp, err := multitenancy.CreateOrUpdateTenant(tenantId, multitenancymodels.TenantConfig{
EmailPasswordEnabled: &emailPasswordEnabled,
ThirdPartyEnabled: &thirdPartyEnabled,
PasswordlessEnabled: &passwordlessEnabled,
})
if err != nil {
// handle error
}
if resp.OK.CreatedNew {
// new tenant was created
} else {
// existing tenant's config was modified.
}
}
- In the above, we are creating a new tenant with the id
"customer1"
. We are also enabling the email password, third party and passwordless login for this tenant. You can also disable any of these by setting the corresponding field tofalse
(which is also the default setting).
- Asyncio
- Syncio
from supertokens_python.recipe.multitenancy.asyncio import create_or_update_tenant
from supertokens_python.recipe.multitenancy.interfaces import TenantConfig
async def some_func():
response = await create_or_update_tenant("customer1", TenantConfig(
email_password_enabled=True,
third_party_enabled=True,
passwordless_enabled=True,
))
if response.status != "OK":
print("Handle error")
elif response.created_new:
print("New tenant was created")
else:
print("Existing tenant's config was updated")
from supertokens_python.recipe.multitenancy.syncio import create_or_update_tenant
from supertokens_python.recipe.multitenancy.interfaces import TenantConfig
response = create_or_update_tenant("customer1", TenantConfig(
email_password_enabled=True,
third_party_enabled=True,
passwordless_enabled=True,
))
if response.status != "OK":
print("Handle error")
elif response.created_new:
print("New tenant was created")
else:
print("Existing tenant's config was updated")
- In the above, we are creating a new tenant with the id
"customer1"
. We are also enabling the email password, third party and passwordless login for this tenant. You can also disable any of these by setting the corresponding field tofalse
(which is also the default setting).
curl --location --request PUT '/appid-<APP_ID>/recipe/multitenancy/tenant' \
--header 'api-key: ' \
--header 'Content-Type: application/json' \
--data-raw '{
"tenantId": "customer1",
"emailPasswordEnabled": true,
"thirdPartyEnabled": true,
"passwordlessEnabled": true
}'
- We specify the appId for which we want to create a new tenant in the URL. If you are using the default (
"public"
) app, you can omit the/appid-<APP_ID>
part of the URL. - In the above, we are creating a new tenant with the id
"customer1"
. We are also enabling the email password, third party and passwordless login for this tenant. You can also disable any of these by setting the corresponding field tofalse
(which is also the default setting).
#
Providing additional configuration per tenantYou can also configure a tenant to have different configurations as per the core's config.yaml
(or docker env) variabls. Below is how you can specify the config, when creating or modifying a tenant:
- NodeJS
- GoLang
- Python
- cURL
Important
import Multitenancy from "supertokens-node/recipe/multitenancy";
async function createNewTenant() {
let resp = await Multitenancy.createOrUpdateTenant("customer1", {
coreConfig: {
"email_verification_token_lifetime": 7200000,
"password_reset_token_lifetime": 3600000,
"postgresql_connection_uri": "postgresql://localhost:5432/db2",
}
});
if (resp.createdNew) {
// new tenant was created
} else {
// existing tenant's config was modified.
}
}
import (
"github.com/supertokens/supertokens-golang/recipe/multitenancy"
"github.com/supertokens/supertokens-golang/recipe/multitenancy/multitenancymodels"
)
func main() {
tenantId := "customer1"
resp, err := multitenancy.CreateOrUpdateTenant(tenantId, multitenancymodels.TenantConfig{
CoreConfig: map[string]interface{}{
"email_verification_token_lifetime": 7200000,
"password_reset_token_lifetime": 3600000,
"postgresql_connection_uri": "postgresql://localhost:5432/db2",
},
})
if err != nil {
// handle error
}
if resp.OK.CreatedNew {
// new tenant was created
} else {
// existing tenant's config was modified.
}
}
- Asyncio
- Syncio
from supertokens_python.recipe.multitenancy.asyncio import create_or_update_tenant
from supertokens_python.recipe.multitenancy.interfaces import TenantConfig
async def some_func():
tenant_id = "customer1"
result = await create_or_update_tenant(tenant_id, TenantConfig(
core_config={
"email_verification_token_lifetime": 7200000,
"password_reset_token_lifetime": 3600000,
"postgresql_connection_uri": "postgresql://localhost:5432/db2",
},
))
if result.status != "OK":
print("handle error")
elif result.created_new:
print("new tenant created")
else:
print("existing tenant's config was modified.")
from supertokens_python.recipe.multitenancy.syncio import create_or_update_tenant
from supertokens_python.recipe.multitenancy.interfaces import TenantConfig
tenant_id = "customer1"
result = create_or_update_tenant(tenant_id, TenantConfig(
core_config={
"email_verification_token_lifetime": 7200000,
"password_reset_token_lifetime": 3600000,
"postgresql_connection_uri": "postgresql://localhost:5432/db2",
},
))
if result.status != "OK":
print("handle error")
elif result.created_new:
print("new tenant created")
else:
print("existing tenant's config was modified.")
- Single app setup
- Multi app setup
curl --location --request PUT '/recipe/multitenancy/tenant' \
--header 'api-key: ' \
--header 'Content-Type: application/json' \
--data-raw '{
"tenantId": "customer1",
"coreConfig": {
"email_verification_token_lifetime": 7200000,
"password_reset_token_lifetime": 3600000,
"postgresql_connection_uri": "postgresql://localhost:5432/db2"
}
}'
curl --location --request PUT '/recipe/multitenancy/tenant' \
--header 'api-key: ' \
--header 'Content-Type: application/json' \
--data-raw '{
"tenantId": "customer1",
"coreConfig": {
"email_verification_token_lifetime": 7200000,
"password_reset_token_lifetime": 3600000,
"postgresql_connection_uri": "postgresql://localhost:5432/db2"
}
}'
In the above example, we are setting different values for certain configs for customer1
tenant. All other configs are inherited from the base config (config.yaml file or docker env vars).
We even specify a postgresql_connection_uri
config. This means that all the information related to this tenant (users, roles, metadata etc) will be saved in the db pointed to by the value of postgresql_connection_uri
(A similar config exists for MySQL as well). This can be used to achieve data isolation on a tenant level. This config is not necessary and if not provided, the tenant's information will be stored in the db as specified in the core's config.yaml or docker env vars (it will still be a different user pool though).
Here is the list of full core config variables that can be configured, and below are the lists of variables depending on the database you use:
important
Some configs cannot be different across tenants - they must be the same within an app. In the above links, if a config has a comment saying DIFFERENT_ACROSS_TENANTS
, then it can be changed for each tenant, else if it has DIFFERENT_ACROSS_APPS
, then it must be the same for all tenants within an app.
If a config has neither of these, then it can only be set per core instance.
Once you have set the configs for a specific tenant, you can fetch the tenant info as shown below:
- NodeJS
- GoLang
- Python
- cURL
Important
import Multitenancy from "supertokens-node/recipe/multitenancy";
async function getTenant(tenantId: string) {
let resp = await Multitenancy.getTenant(tenantId);
if (resp === undefined) {
// tenant does not exist
} else {
let coreConfig = resp.coreConfig;
let isEmailPasswordLoginEnabled = resp.emailPassword.enabled;
let isThirdPartyLoginEnabled = resp.thirdParty.enabled;
let isPasswordlessLoginEnabled = resp.passwordless.enabled;
let configuredThirdPartyProviders = resp.thirdParty.providers;
}
}
import (
"fmt"
"github.com/supertokens/supertokens-golang/recipe/multitenancy"
)
func main() {
tenantId := "customer1"
tenant, err := multitenancy.GetTenant(tenantId)
if err != nil {
// handle error
}
if tenant == nil {
// tenant does not exist
} else {
isEmailPasswordLoginEnabled := tenant.EmailPassword.Enabled;
isThirdPartyLoginEnabled := tenant.ThirdParty.Enabled;
isPasswordlessLoginEnabled := tenant.Passwordless.Enabled;
if (isEmailPasswordLoginEnabled) {
// Tenant support email password login
}
if (isThirdPartyLoginEnabled) {
// Tenant support third party login
configuredThirdPartyProviders := tenant.ThirdParty.Providers;
fmt.Println(configuredThirdPartyProviders);
}
if (isPasswordlessLoginEnabled) {
// Tenant support passwordless login
}
}
}
- Asyncio
- Syncio
from supertokens_python.recipe.multitenancy.asyncio import get_tenant
async def some_func():
tenant = await get_tenant("customer1")
if tenant is None:
print("tenant does not exist")
else:
is_email_login_enabled = tenant.emailpassword.enabled
is_third_party_login_enabled = tenant.third_party.enabled
is_passwordless_login_enabled = tenant.passwordless.enabled
if is_email_login_enabled:
print("Tenant supports email password login")
if is_third_party_login_enabled:
print("Tenant supports third party login")
configured_providers = tenant.third_party.providers
print(configured_providers)
if is_passwordless_login_enabled:
print("Tenant supports passwordless login")
from supertokens_python.recipe.multitenancy.syncio import get_tenant
tenant = get_tenant("customer1")
if tenant is None:
print("tenant does not exist")
else:
is_email_login_enabled = tenant.emailpassword.enabled
is_third_party_login_enabled = tenant.third_party.enabled
is_passwordless_login_enabled = tenant.passwordless.enabled
if is_email_login_enabled:
print("Tenant supports email password login")
if is_third_party_login_enabled:
print("Tenant supports third party login")
configured_providers = tenant.third_party.providers
print(configured_providers)
if is_passwordless_login_enabled:
print("Tenant supports passwordless login")
- Single app setup
- Multi app setup
curl --location --request GET '/customer1/recipe/multitenancy/tenant' \
--header 'api-key: ' \
--header 'Content-Type: application/json'
curl --location --request GET '/customer1/recipe/multitenancy/tenant' \
--header 'api-key: ' \
--header 'Content-Type: application/json'
Notice that we added customer1
to the path of the request. This tells the core that the tenant you want to get the information about is customer1
(the one we created before in this page).
If the input tenant does not exist, you will get back a 200
status code with the following JSON:
{status: "TENANT_NOT_FOUND_ERROR"}
Otherwise you will get a 200
status code with the following JSON output:
{
"status": "OK",
"emailPassword": {
"enabled": boolean
},
"thirdParty": {
"enabled": boolean,
"providers": [...]
},
"passwordless": {
"enabled": boolean
},
"coreConfig": {
"email_verification_token_lifetime": 7200000,
"password_reset_token_lifetime": 3600000,
"postgresql_connection_uri": "postgresql://localhost:5432/db2"
},
"tenantId": "customer1"
}
The returned coreConfig
is the same as what we had set when creating / updating the tenant. The rest of the core configurations for this tenant are inherited from the app's (or the public
tenant) config. The public
tenant, for the public
app inherits its configs from the config.yaml
/ docker env var values.
#
Next stepsCheckout the "Setting up login for tenants" page for next steps in integrating multi tenancy.