Node.js client for Permify

Overview

Permify Node.js Client

npm Twitter URL

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.

Permify Documentation

See more

You might also like...

Lightweight universal Cloudflare API client library for Node.js, Browser, and CF Workers

Cloudflare API Client Lightweight universal HTTP client for Cloudflare API based on Fetch API that works in Node.js, browser, and CF Workers environme

Nov 13, 2022

QuestDB Node.js Client

QuestDB Node.js Client Installation npm install @questdb/nodejs-client Examples Basic API usage const { Sender } = require("@questdb/nodejs-client");

Dec 22, 2022

Yet Another Clickhouse Client for Node.js

yacc-node - Yet Another Clickhouse Client for NodeJS Introduction yacc-node is a zero depencies Clickhouse Client written in Typescript. Installation

Nov 3, 2022

Node-cli-starter - Basic starter kit for building Node CLI applications with TypeScript.

node-cli-starter Minimal starter kit for building Node CLI applications with TypeScript. Getting Started To get started clone repo locally and run npm

May 17, 2022

Apilytics for Node.js - Easy API analytics for Node backends

apilytics-node Apilytics is a service that lets you analyze operational, performance and security metrics from your APIs without infrastructure-level

Sep 2, 2022

This is a vanilla Node.js rest API created to show that it is possible to create a rest API using only vanilla Node.js

This is a vanilla Node.js rest API created to show that it is possible to create a rest API using only vanilla Node.js. But in most cases, I would recommend you to use something like Express in a production project for productivity purposes.

Jul 19, 2022

Inter Process Communication Module for node supporting Unix sockets, TCP, TLS, and UDP. Giving lightning speed on Linux, Mac, and Windows. Neural Networking in Node.JS

Inter Process Communication Module for node supporting Unix sockets, TCP, TLS, and UDP. Giving lightning speed on Linux, Mac, and Windows. Neural Networking in Node.JS

Inter Process Communication Module for node supporting Unix sockets, TCP, TLS, and UDP. Giving lightning speed on Linux, Mac, and Windows. Neural Networking in Node.JS

Dec 9, 2022

Node js package makes creating node jd dependincies files like Controllers,Entities and Repositories easier by executing a few instructions

Node js package makes creating node jd dependincies files like Controllers,Entities and Repositories easier by executing a few instructions

Nodejs Studio Node js package makes creating node js project dependincies files like Controllers,Entities and Repositories easier by executing a few i

Oct 12, 2022

Spin node create spin api for node

Links Contract api JS api @spinfi/core @spinfi/node @spinfi/node Spin node create spin api for node How to install yarn add @spinfi/node How to init i

Oct 18, 2022
Owner
Permify
Permify is a plug-&-play authorization API that helps dev teams create granular access control and user management systems without breaking a sweat!
Permify
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Unofficial API client for the Tidbyt API. Use this client to control Tidbyt devices and integrate with other services.

Tidbyt Client for Node.js Unofficial API client for the Tidbyt API. Use this client to control Tidbyt devices and integrate with other services. Insta

Nicholas Penree 19 Dec 17, 2022
Node 18's node:test, as a node module

node-core-test This is a user-land port of node:test, the experimental test runner introduced in Node.js 18. This module makes it available in Node.js

Julian Gruber 62 Dec 15, 2022
Javascript client for Sanity. Works in node.js and modern browsers (older browsers needs a Promise polyfill).

@sanity/client Javascript client for Sanity. Works in node.js and modern browsers (older browsers needs a Promise polyfill). Requirements Sanity Clien

Sanity 23 Nov 29, 2022
Official Node.js client library for Devzat plugin API

Devzat plugin API client for Node.js This NPM package allows you to build Devzat plugins/bots with JavaScript/TypeScript. See example/index.ts for a f

Benjamin Smith 2 Apr 26, 2022
A Node.js client & server implementation of the WAMP-like RPC-over-websocket system defined in the OCPP-J protcols.

OCPP-RPC A client & server implementation of the WAMP-like RPC-over-websocket system defined in the OCPP-J protcols (e.g. OCPP1.6J and OCPP2.0.1J). Re

Mikuso 14 Dec 30, 2022
Postgres.js - The Fastest full featured PostgreSQL client for Node.js and Deno

?? Fastest full-featured node & deno client ?? ES6 Tagged Template Strings at the core ??‍♀️ Simple surface API ??️ Dynamic query support ?? Chat and

Rasmus Porsager 4.3k Jan 1, 2023
Node.js implementation of the Socket SDK client

SYNOPSIS A Node.js adapter for the Socket SDK DESCRIPTION Socket SDK uses a simple uri-based protocol for brokering messages between the render proces

Socket Supply Co. 11 Dec 28, 2022
Promise based HTTP client for the browser and node.js

axios Promise based HTTP client for the browser and node.js New axios docs website: click here Table of Contents Features Browser Support Installing E

axios 98k Jan 5, 2023