Permify Node.js Client
Use Permify in server-side Node.js Projects.
Install
npm install @permify/permify-node
Usage
To get started, create Permify client using your Workspace id and API private key:
with require
const Permify = require("@permify/permify-node");
const permifyClient = new Permify.Client("workspace_id", "private_token");
with import
import { Client as PermifyClient } from "@permify/permify-node";
const permifyClient = new PermifyClient("workspace_id", "private_token");
Create Group
In order to start using Permify you need to have at least one group entity which is part of your workspace. This method creates a group entity in Permify.
const payload = {
//If your app is a multi-tenant, id represents your group or tenant's id.
//If your app is not multi-tenant or haven't got group entity, dont send id
id: 'test_cs1234utg',
//Name of the group
name: 'test_name'
}
permifyClient.createGroup(payload)
.then((group) => {
console.log(group)
})
.catch((error) => {
console.log(error.response)
});
Create User
This method creates a user entity in Permify. You need a group, which the created user must belong.
const payload = {
//Id of the user to be created in your app
id: "id",
//id of group
group_id: "group id",
// name of the user (optional)
name: "name",
// photo url of the user (optional)
photo: "",
// role names array (optional)
role_names: [],
// custom attributes (optional)
attributes: {}
};
permifyClient.createUser(payload)
.then((user) => {
console.log(user)
})
.catch((error) => {
console.log(error.response)
});
Create Role
This method creates a role entity in Permify.
permifyClient.createRole('group_id', {name: 'test_role'})
.then((role) => {
console.log(role)
})
.catch((error) => {
console.log(error.response)
});
Create Rule
This method creates a rule entity in Permify. For more details checkout Creating Rule.
Sample Rules
Is the user senior?
user.attributes.tenure > 8
Is the user manager?
"manager" in user.roles
Is the user admin?
"admin" in user.roles
Is the user the owner of the resource?
user.id == resource.attributes.owner_id
const payload = {
// name of rule
name: "name",
// conditions
condition: "condition string"
};
permifyClient.createRule(payload)
.then((rule) => {
console.log(rule)
})
.catch((error) => {
console.log(error.response.data)
});
Create Option
This method creates a option entity in Permify.
const payload = {
// name of option
name: "name",
// rule names
rule_names: []
};
permifyClient.createOption(payload)
.then((option) => {
console.log(option)
})
.catch((error) => {
console.log(error.response.data)
});
Create Policy
This method creates a policy entity in Permify.
const payload = {
// name of policy
name: "name",
// rule names
option_names: []
};
permifyClient.createPolicy(payload)
.then((policy) => {
console.log(policy)
})
.catch((error) => {
console.log(error.response.data)
});
IsAuthorized
This method returns a decision about whether the user is authorized for this action with the given parameters.
Parameters
PolicyName
(mandatory)
Custom Permify Policy name.
UserID
(mandatory)
Id of the User
ResourceID
(optional)
Id of the Resource, mandatory if any resource used or accessed when creating Rule/Rules.
ResourceType
(optional)
Type or name of the Resource, mandatory if any resource used or accessed when creating Rule/Rules.
const payload = {
// name of policy
policy_name: "policy name",
// id of user
user_id: "user id",
// resource (optional)
resource_id: "resource id",
resource_type: "resource type"
};
permifyClient.isAuthorized(payload)
.then((res) => {
if(res.data.allow){
// action
}
})
For more information on how to use the Permify API, please refer to the Permify API Reference.