permit-golang

command module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 6 Imported by: 0

README

Golang.png

Go SDK for Permit.io

Instructions for installing Go SDK for interacting with Permit.io.

Overview

This guide will walk you through the steps of installing the Permit.io Go SDK and integrating it into your code.

Installation

Install the SDK using the following command:

go get github.com/permitio/permit-golang

The SDK requires Go 1.19 or later. CI tests it on Go 1.19 and Go 1.27.

Put the package under your project folder and add the following in import:

import "github.com/permitio/permit-golang/pkg/permit"

Usage

Init the SDK

To init the SDK, you need to create a new Permit client with the API key you got from the Permit.io dashboard.

First we will create a new Config object so we can pass it to the Permit client.

Second, we will create a new Permit client with the Config object we created.

package main

import "github.com/permitio/permit-golang/pkg/permit"
import "github.com/permitio/permit-golang/pkg/config"

func main() {
	PermitConfig := config.NewConfigBuilder("<YOUR_API_TOKEN>").Build()
	Permit := permit.New(PermitConfig)
	_ = Permit // call the methods shown below on Permit
}
API URL and PDP URL

The client sends requests to two URLs, the API URL and the PDP URL. An OPA URL, if you set one, replaces the PDP URL:

  • The API URL, set with WithApiUrl, defaults to https://api.permit.io. SyncUser and every method under Permit.Api and Permit.Elements use it, as does the request that reads the project and environment of your API key.
  • The PDP URL, set with WithPdpUrl, defaults to http://localhost:7766, a PDP running on the same machine. Only Check, BulkCheck, FilterObjects, AllTenantsCheck, GetUserPermissions and GetUserPermissionsWithOptions use it. If you set an OPA URL with WithOpaUrl, these methods use that URL instead.
API key levels

An API key belongs to an environment, a project or an organization. Methods that work in an environment, such as SyncUser and the Permit.Api.Users, Permit.Api.Tenants and Permit.Api.Roles methods, need the client to know the project and the environment:

  • Environment API key: nothing to set. The client reads the project and the environment from the key on its first call.
  • Project API key: call Permit.Api.SetContext(ctx, "<project>", "<environment>") with the key's project and the environment to use.
  • Organization API key: call Permit.Api.SetContext(ctx, "<project>", "<environment>") with the project and the environment to use. The Permit.Api.Projects methods do not need it.

Call SetContext once, after permit.New and before the other calls. It does not return an error: when it fails, it logs the error and clears the project and environment that an earlier call set.

Check for permissions

To check permissions with the Permit.Check() method, build a user and a resource with the builders in the github.com/permitio/permit-golang/pkg/enforcement package.

Follow the example below:

package main

import (
	"fmt"

	"github.com/permitio/permit-golang/pkg/config"
	"github.com/permitio/permit-golang/pkg/enforcement"
	"github.com/permitio/permit-golang/pkg/permit"
)

func main() {
	permitConfig := config.NewConfigBuilder("<YOUR_API_TOKEN>").Build()
	Permit := permit.New(permitConfig)

	user := enforcement.UserBuilder("user_id").Build()
	resource := enforcement.ResourceBuilder("resource_type").Build()
	permitted, err := Permit.Check(user, "read", resource)
	if err != nil {
		// The check failed, for example because the PDP could not be reached; this is not a deny.
		fmt.Println(err)
		return
	}
	if permitted {
		// Let the user read the resource
	} else {
		// Deny access
	}
}
Sync users

To sync users to the Permit.io API, using the Permit.SyncUser() method, you will have to create a User model using our main Models package. The models are located in github.com/permitio/permit-golang/pkg/models

Follow the example below:

package main

import (
	"context"
	"fmt"

	"github.com/permitio/permit-golang/pkg/config"
	"github.com/permitio/permit-golang/pkg/models"
	"github.com/permitio/permit-golang/pkg/permit"
)

func main() {
	ctx := context.Background()
	PermitConfig := config.NewConfigBuilder("<YOUR_API_TOKEN>").Build()
	Permit := permit.New(PermitConfig)

	newUser := models.NewUserCreate("new_user")
	user, err := Permit.SyncUser(ctx, *newUser)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(user.Key)
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal
redact
Package redact removes credentials from text that the SDK prints, logs or keeps in errors.
Package redact removes credentials from text that the SDK prints, logs or keeps in errors.
testcleanup
Package testcleanup deletes what one run of the integration tests in pkg/tests created in a Permit environment.
Package testcleanup deletes what one run of the integration tests in pkg/tests created in a Permit environment.
pkg
api

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL