Get a tenant with the Go SDK
Permit.Api.Tenants.Get() fetches one tenant and its details from the environment that your API key belongs to. This reference is for Go developers who read tenant data from backend code. Use Get or GetByKey when you have the tenant key, and GetById when you have the tenant ID (a UUID).
Get a tenant by key with Tenants.Get or Tenants.GetByKey
Tenants.GetByKey calls Tenants.Get with the same arguments, so the two methods behave the same.
func (t *Tenants) Get(ctx context.Context, tenantKey string) (*models.TenantRead, error)
func (t *Tenants) GetByKey(ctx context.Context, tenantKey string) (*models.TenantRead, error)
Tenants.Get parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx | context.Context | Yes | The context of the request. |
tenantKey | string | Yes | The key of the tenant. |
Example: get a tenant 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 tenantKey with the key of your tenant:
tenant, err := Permit.Api.Tenants.Get(ctx, "tenantKey")
Get a tenant by ID with Tenants.GetById
Tenants.GetById takes the tenant ID as a uuid.UUID from the github.com/google/uuid package, converts it to a string, and calls Tenants.Get.
func (t *Tenants) GetById(ctx context.Context, tenantId uuid.UUID) (*models.TenantRead, error)
Tenants.GetById parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx | context.Context | Yes | The context of the request. |
tenantId | uuid.UUID | Yes | The ID of the tenant. |
Example: get a tenant by ID
Pass the tenant ID as a uuid.UUID value. Parse an ID string with uuid.MustParse:
tenantId := uuid.MustParse("<tenant-id>")
tenant, err := Permit.Api.Tenants.GetById(ctx, tenantId)
Tenant return value and errors
On success, each method returns a *models.TenantRead with the tenant's Key, Id, Name, Description, 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:
ErrorCode | Cause |
|---|---|
NotFound | HTTP 404: no tenant with that key or ID exists in the environment. |
Unauthorized, ForbiddenAccess | HTTP 401 or 403: the API key is invalid or has no access to the environment. |
UnexpectedError | A server error (HTTP 5xx) or a network error. |