Skip to main content

Get a user with the Go SDK

Permit.Api.Users.Get() fetches one user and its details from the environment that your API key belongs to. This reference is for Go developers who read user data from backend code. Use Get or GetByKey when you have the user key, and GetById when you have the user ID (a UUID).

Get a user by key with Users.Get or Users.GetByKey

Users.GetByKey calls Users.Get with the same arguments, so the two methods behave the same.

func (u *Users) Get(ctx context.Context, userKey string) (*models.UserRead, error)

func (u *Users) GetByKey(ctx context.Context, userKey string) (*models.UserRead, error)

Users.Get parameters

ParameterTypeRequiredDescription
ctxcontext.ContextYesThe context of the request.
userKeystringYesThe key of the user.

Example: get a user by key

The example uses a client named Permit, created with permit.NewPermit() as shown in Check permissions with the Go SDK, and a ctx of type context.Context. Replace user-key with the key of your user:

user, err := Permit.Api.Users.Get(ctx, "user-key")

Get a user by ID with Users.GetById

Users.GetById takes the user ID as a uuid.UUID from the github.com/google/uuid package, converts it to a string, and calls Users.Get.

func (u *Users) GetById(ctx context.Context, userId uuid.UUID) (*models.UserRead, error)

Users.GetById parameters

ParameterTypeRequiredDescription
ctxcontext.ContextYesThe context of the request.
userIduuid.UUIDYesThe ID that Permit assigned to the user. This is not the user key.

Example: get a user by ID

Call Users.GetById and pass the user ID as a uuid.UUID value. Parse an ID string with uuid.MustParse:

userId := uuid.MustParse("<user-id>")
user, err := Permit.Api.Users.GetById(ctx, userId)

User return value and errors

On success, each method returns a *models.UserRead with the user's Key, Id, Email, FirstName, LastName, and Attributes.

If the call fails, err holds an errors.PermitError from the github.com/permitio/permit-golang/pkg/errors package. Its StatusCode field has the HTTP status, and its ErrorCode field has one of these codes:

ErrorCodeCause
NotFoundHTTP 404: no user with that key or ID exists in the environment.
Unauthorized, ForbiddenAccessHTTP 401 or 403: the API key is invalid or has no access to the environment.
UnexpectedErrorA server error (HTTP 5xx) or a network error.