Skip to main content

Create a tenant with the Java SDK

permit.api.tenants.create() creates a tenant in your Permit environment. A tenant is an isolated group of users and resources, such as one customer organization in a multi-tenant application. This reference is for Java developers who create tenants from code. The examples assume an initialized Permit client named permit, as set up in Check permissions with the Java SDK.

Signature

TenantRead create(TenantCreate tenantData) throws IOException, PermitApiError, PermitContextError

Parameters

ParameterTypeRequiredDescription
tenantDataTenantCreateYesThe tenant to create. The fields are listed in TenantCreate fields.

TenantCreate fields

The TenantCreate(key, name) constructor sets the two required fields. Set the optional fields with the with...() builder methods.

FieldBuilder methodTypeRequiredDescription
keywithKey()StringYesA unique, URL-friendly (slugified) ID by which Permit identifies the tenant.
namewithName()StringYesA descriptive name for the tenant.
descriptionwithDescription()StringNoA longer description of the tenant.
attributeswithAttributes()HashMap<String, Object>NoTenant attributes for attribute-based access control (ABAC) policies.

Example

The example creates a tenant with a key, a name, a description, and attributes. The attributes are in a tenantAttributes map (a java.util.HashMap) that the example passes to withAttributes().

import io.permit.sdk.openapi.models.TenantCreate;
import io.permit.sdk.openapi.models.TenantRead;
import java.util.HashMap;

// optional attributes for attribute-based access control
HashMap<String, Object> tenantAttributes = new HashMap<>();
tenantAttributes.put("age", Integer.valueOf(50));
tenantAttributes.put("fav_color", "red");

TenantRead tenant = permit.api.tenants.create(
new TenantCreate("acme-corp", "Acme Corp")
.withDescription("An example customer")
.withAttributes(tenantAttributes)
);

Return value

create() returns a TenantRead object (io.permit.sdk.openapi.models.TenantRead) with the created tenant, including the ID that Permit assigns to the tenant.

Exceptions

All exception classes except IOException are in the io.permit.sdk.api package.

ExceptionThrown when
IOExceptionThe HTTP request to the Permit API fails, for example because of a network error.
PermitApiErrorThe Permit API returns an error status code. getResponseCode() returns the HTTP status code and getRawResponse() returns the response body.
PermitContextErrorThe SDK context does not include an environment, for example because the API key is scoped to an organization or project.