Fetch a tenant with the Java SDK
permit.api.tenants.get() fetches one tenant from 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 read tenants from code. The examples assume an initialized Permit client named permit, as set up in Check permissions with the Java SDK.
Signature
The SDK has three methods that fetch a tenant:
| Method | Signature |
|---|---|
get() | TenantRead get(String tenantKey) |
getByKey() | TenantRead getByKey(String tenantKey) |
getById() | TenantRead getById(UUID tenantId) |
All three methods throw IOException, PermitApiError, and PermitContextError. getByKey() and getById() call get() with the key or the ID as a string.
Parameters
| Parameter | Type | Method | Description |
|---|---|---|---|
tenantKey | String | get(), getByKey() | The key or the ID (UUID) of the tenant. The Permit API accepts either value. |
tenantId | java.util.UUID | getById() | The ID of the tenant. |
Example
Replace [TENANT ID OR KEY], [TENANT KEY], and [TENANT ID] with values from your environment.
import io.permit.sdk.openapi.models.TenantRead;
import java.util.UUID;
// accepts both the tenant id and the tenant key
TenantRead tenant = permit.api.tenants.get("[TENANT ID OR KEY]");
// if you want to be explicit, you can use getByKey
TenantRead tenant2 = permit.api.tenants.getByKey("[TENANT KEY]");
// or getById
TenantRead tenant3 = permit.api.tenants.getById(UUID.fromString("[TENANT ID]"));
Return value
Each method returns a TenantRead object (io.permit.sdk.openapi.models.TenantRead) with the tenant's key, name, ID, description, and attributes.
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. If no tenant matches the key or ID, the 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. |