Fetch a user with the Java SDK
permit.api.users.get() fetches one user from your Permit environment. This reference is for Java developers who read Permit users 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 user:
| Method | Signature |
|---|---|
get() | UserRead get(String userKey) |
getByKey() | UserRead getByKey(String userKey) |
getById() | UserRead getById(UUID userId) |
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 |
|---|---|---|---|
userKey | String | get(), getByKey() | The key or the ID (UUID) of the user. The Permit API accepts either value. |
userId | java.util.UUID | getById() | The ID of the user. |
Example
Replace [USER ID OR KEY], [USER KEY], and [USER ID] with values from your environment.
import io.permit.sdk.openapi.models.UserRead;
import java.util.UUID;
// accepts both the user id and the user key
UserRead user = permit.api.users.get("[USER ID OR KEY]");
// if you want to be explicit, you can use getByKey
UserRead user2 = permit.api.users.getByKey("[USER KEY]");
// or getById
UserRead user3 = permit.api.users.getById(UUID.fromString("[USER ID]"));
Return value
Each method returns a UserRead object (io.permit.sdk.openapi.models.UserRead) with the user's key, ID, email, first name, last name, 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 user 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. |