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
| Parameter | Type | Required | Description |
|---|---|---|---|
tenantData | TenantCreate | Yes | The 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.
| Field | Builder method | Type | Required | Description |
|---|---|---|---|---|
key | withKey() | String | Yes | A unique, URL-friendly (slugified) ID by which Permit identifies the tenant. |
name | withName() | String | Yes | A descriptive name for the tenant. |
description | withDescription() | String | No | A longer description of the tenant. |
attributes | withAttributes() | HashMap<String, Object> | No | Tenant 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.
| Exception | Thrown when |
|---|---|
IOException | The HTTP request to the Permit API fails, for example because of a network error. |
PermitApiError | The Permit API returns an error status code. getResponseCode() returns the HTTP status code and getRawResponse() returns the response body. |
PermitContextError | The SDK context does not include an environment, for example because the API key is scoped to an organization or project. |