Create a tenant with the Go SDK
Permit.Api.Tenants.Create() creates a tenant in the environment that your API key belongs to. This reference is for Go developers who manage tenants from backend code.
Tenants.Create signature
func (t *Tenants) Create(ctx context.Context, tenantCreate models.TenantCreate) (*models.TenantRead, error)
Tenants.Create parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx | context.Context | Yes | The context of the request. |
tenantCreate | models.TenantCreate | Yes | The tenant to create. Build it with models.NewTenantCreate(key, name). |
TenantCreate fields
| Field | Type | Required | Description |
|---|---|---|---|
Key | string | Yes | A unique ID by which Permit identifies the tenant. The key must be URL-friendly (slugified). |
Name | string | Yes | A descriptive name for the tenant. |
Description | *string | No | A longer description of the tenant. Set it with SetDescription(). |
Attributes | map[string]interface{} | No | Tenant attributes that attribute-based access control (ABAC) policies evaluate. Set them with SetAttributes(). |
Example: create a tenant with Tenants.Create
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, such as context.Background().
Build the TenantCreate struct. NewTenantCreate() sets the key tenant-key and the name tenant-name. The SetName() call then replaces the name with tenant-name-new:
tenantCreate := models.NewTenantCreate("tenant-key", "tenant-name")
tenantCreate.SetName("tenant-name-new")
Pass the struct to Tenants.Create:
tenant, err := Permit.Api.Tenants.Create(ctx, *tenantCreate)
Tenants.Create return value and errors
On success, Tenants.Create returns a *models.TenantRead with the tenant's Key, Id, Name, Description, and Attributes. If a tenant with the same key already exists, the Permit API returns the existing tenant instead of an error.
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 |
|---|---|
UnprocessableEntityError | HTTP 422: a field failed validation, for example a key that isn't URL-friendly. |
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. |