Skip to main content

Create a resource with the Go SDK

Permit.Api.Resources.Create() creates a resource, the type of object you protect with permissions (such as document), in the environment that your API key belongs to. This reference is for Go developers who define their policy schema from backend code. After you create a resource, grant its actions to roles with Roles.Create or Roles.Update.

Resources.Create signature

func (r *Resources) Create(ctx context.Context, resourceCreate models.ResourceCreate) (*models.ResourceRead, error)

Resources.Create parameters

ParameterTypeRequiredDescription
ctxcontext.ContextYesThe context of the request.
resourceCreatemodels.ResourceCreateYesThe resource to create. Build it with models.NewResourceCreate(key, name, actions).

ResourceCreate fields

FieldTypeRequiredDescription
KeystringYesA URL-friendly name of the resource (a slug). You use the key to refer to the resource in later calls, and in permissions such as document:read, instead of the resource ID (a UUID).
NamestringYesThe name of the resource.
Actionsmap[string]models.ActionBlockEditableYesThe actions users can perform on the resource. Each map key is an action key, such as edit. Each value is an ActionBlockEditable built with models.NewActionBlockEditable(), with an optional Name, Description, and Attributes.
Urn*stringNoThe Uniform Resource Name (URN) of the resource. Set it with SetUrn().
Description*stringNoA longer description of what the resource represents in your system. Set it with SetDescription().
Attributes*map[string]models.AttributeBlockEditableNoThe attributes that each resource of this type defines, for use in attribute-based access control (ABAC) policies. Set them with SetAttributes().

The Go ResourceCreate struct has no fields for resource roles or relations.

Example: create a resource with Resources.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.

Build an edit action, and pass it in the actions map of a new ResourceCreate struct with the key resource-key. The final SetName("document") call replaces the name resource-name that the constructor set:

action := models.NewActionBlockEditable()
action.SetName("edit")
resourceCreate := models.NewResourceCreate("resource-key", "resource-name", map[string]models.ActionBlockEditable{"edit": *action})
resourceCreate.SetName("document")

Pass the struct to Resources.Create:

resource, err := Permit.Api.Resources.Create(ctx, *resourceCreate)

Resources.Create return value and errors

On success, Resources.Create returns a *models.ResourceRead with the resource's Key, Id, Name, Actions, 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
UnprocessableEntityErrorHTTP 422: a field failed validation, for example a missing Actions map.
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.