This client makes it easy to interact with Permify from your Node.js application.
Use npm to install:
npm config set @buf:registry https://buf.build/gen/npm/v1/
npm install @permify/permify-node
Use yarn to install:
yarn config set npmScopes.buf.npmRegistryServer https://buf.build/gen/npm/v1/
yarn add @permify/permify-node
import * as permify from "@permify/permify-node";
const request = new permify.grpc.payload.TenantCreateRequest();
request.setId("t1");
request.setName("Tenant 1");
const client = permify.grpc.newClient({
endpoint: "localhost:3478",
cert: undefined
});
client.tenancy.create(request).then((response) => {
console.log(response);
// handle response
})
import * as permify from "@permify/permify-node";
const client = permify.grpc.newClient({
endpoint: "localhost:3478",
cert: undefined
});
// Define the schema
let schema = `
entity user {}
entity document {
relation viewer @user
action view = viewer
}
`;
// Create and set the SchemaWriteRequest
let schemaWriteRequest = new permify.grpc.payload.SchemaWriteRequest();
schemaWriteRequest.setTenantId("t1");
schemaWriteRequest.setSchema(schema);
// Write the schema
client.schema.write(schemaWriteRequest).then((response) => {
// handle response
})
import * as permify from "@permify/permify-node";
const client = permify.grpc.newClient({
endpoint: "localhost:3478",
cert: undefined
});
// Create and set the RelationshipWriteRequest
let relationshipWriteRequest = new permify.grpc.payload.SchemaWriteRequest();
relationshipWriteRequest.setTenantId("t1");
relationshipWriteRequest.setMetadata(new permify.grpc.payload.SchemaWriteRequestMetadata());
// Create the tuple list
let tupleList = [];
let tuples = [{
entity: {
type: "document",
id: "1"
},
relation: "viewer",
subject: {
type: "user",
id: "1"
}
}];
tuples.forEach(t => {
let tuple = new permify.grpc.payload.Tuple();
let entity = new permify.grpc.payload.Entity();
entity.setType(t.entity.type);
entity.setId(t.entity.id);
let subject = new permify.grpc.payload.Subject();
subject.setType(t.subject.type);
subject.setId(t.subject.id);
tuple.setEntity(entity);
tuple.setRelation(t.relation);
tuple.setSubject(subject);
tupleList.push(tuple);
});
relationshipWriteRequest.setTuplesList(tupleList);
client.relationship.write(relationshipWriteRequest).then((response) => {
// handle response
})
import * as permify from "@permify/permify-node";
const client = permify.grpc.newClient({
endpoint: "localhost:3478",
cert: undefined
});
// Create and set the PermissionCheckRequest
let permissionCheckRequest = new permify.grpc.payload.PermissionCheckRequest();
permissionCheckRequest.setTenantId("t1");
permissionCheckRequest.setMetadata(new permify.grpc.payload.PermissionCheckRequestMetadata());
// Set the entity details
let permissionCheckRequestEntity = new permify.grpc.payload.Entity();
permissionCheckRequestEntity.setType("document");
permissionCheckRequestEntity.setId("1");
permissionCheckRequest.setEntity(permissionCheckRequestEntity);
// Set the permission to check
permissionCheckRequest.setPermission("view");
// Set the subject details
let permissionCheckRequestSubject = new permify.grpc.payload.Subject();
permissionCheckRequestSubject.setType("user");
permissionCheckRequestSubject.setId("3");
permissionCheckRequest.setSubject(permissionCheckRequestSubject);
// Perform the permission check
client.permission.check(permissionCheckRequest).then((response: permify.grpc.payload.PermissionCheckResponse) => {
if (response.can === permify.grpc.base.CheckResult.CHECK_RESULT_ALLOWED) {
console.log("RESULT_ALLOWED")
} else {
console.log("RESULT_DENIED")
}
});
import * as permify from "@permify/permify-node";
function main() {
const client = permify.grpc.newClient({
endpoint: "localhost:3478",
cert: undefined
});
// Create and set the PermissionLookupEntityRequest
let lookupEntityStreamRequest = new permify.grpc.payload.PermissionLookupEntityRequest();
lookupEntityStreamRequest.setTenantId("t1");
// Set the request metadata
lookupEntityStreamRequest.setMetadata(new permify.grpc.payload.PermissionLookupEntityRequestMetadata());
// Set the entity type and permission
lookupEntityStreamRequest.setEntityType("document");
lookupEntityStreamRequest.setPermission("view");
// Set the subject details
let subject = new permify.grpc.payload.Subject();
subject.setType("user");
subject.setId("1");
lookupEntityStreamRequest.setSubject(subject);
// Perform the lookup entity stream
const res = client.permission.lookupEntityStream(lookupEntityStreamRequest);
handle(res)
}
async function handle(res: AsyncIterable<permify.grpc.payload.PermissionLookupEntityStreamResponse>) {
for await (const response of res) {
// response.toObject().entityId
}
}
import * as permify from "@permify/permify-node";
const client = new permify.grpc.newClient({
endpoint: "localhost:3478",
}, permify.grpc.newAccessTokenInterceptor("YOUR_TOKEN"))
import * as permify from "@permify/permify-node";
import fs from 'fs';
const cert = fs.readFileSync('path/to/cert.pem');
const client = new permify.grpc.newClient({
endpoint: "localhost:3478",
cert: cert,
}, permify.grpc.newAccessTokenInterceptor("YOUR_TOKEN"))
Permify is an open-source authorization service for creating and maintaining fine-grained authorizations accross your individual applications and services.
Join our Discord channel for issues, feature requests, feedbacks or anything else. We love to talk about authorization and access control ❤️