Skip to main content

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​

ParameterTypeRequiredDescription
ctxcontext.ContextYesThe context of the request.
tenantCreatemodels.TenantCreateYesThe tenant to create. Build it with models.NewTenantCreate(key, name).

TenantCreate fields​

FieldTypeRequiredDescription
KeystringYesA unique ID by which Permit identifies the tenant. The key must be URL-friendly (slugified).
NamestringYesA descriptive name for the tenant.
Description*stringNoA longer description of the tenant. Set it with SetDescription().
Attributesmap[string]interface{}NoTenant 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:

ErrorCodeCause
UnprocessableEntityErrorHTTP 422: a field failed validation, for example a key that isn't URL-friendly.
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.