Permify TypeScript Client

raw JSON →
1.6.9 verified Thu Apr 23 auth: no javascript

permify-typescript is the official TypeScript client for Permify, an open-source authorization service designed for building fine-grained and scalable authorization systems. This package provides a programmatic interface to interact with the Permify API, covering operations for schema, relationships, permissions, and tenancy. As of version 1.6.9, the client's versioning now directly aligns with the upstream Permify API/OpenAPI version, ensuring closer synchronization with the service's capabilities. It is automatically generated by the OpenAPI Generator project, which ensures a consistent and up-to-date interface to the Permify API. The library supports Node.js environments version 18 and higher, shipping with full TypeScript type definitions for an enhanced developer experience.

error Error: connect ECONNREFUSED 127.0.0.1:3476
cause The Permify API server is not running or is inaccessible at the specified `basePath` in the client configuration, preventing a connection.
fix
Verify that the Permify server is running and listening on the configured address and port (e.g., http://localhost:3476). Check firewall settings or network connectivity issues.
error Error: Request failed with status code 401
cause The Permify API rejected the request due to missing, invalid, or expired authentication credentials provided in the `Authorization` header.
fix
Double-check that your PERMIFY_API_KEY environment variable (or hardcoded token) is correct and that the Authorization header is properly formatted (e.g., Bearer YOUR_TOKEN).
error TypeError: permify.Configuration is not a constructor
cause This error often occurs when attempting to use CommonJS `require` syntax or incorrect destructuring for a module that primarily uses ES Modules (ESM) and named exports.
fix
Ensure you are using ESM import statements (e.g., import { Configuration } from 'permify-typescript'; or import * as permify from 'permify-typescript';) and that your Node.js project is configured to run ESM (e.g., by setting "type": "module" in your package.json).
gotcha Starting with v1.6.9, `permify-typescript` versions directly align with the upstream Permify API/OpenAPI version. A jump in the client's major or minor version number does not necessarily imply a breaking change in the SDK itself, but rather an update to match the Permify service's capabilities. Always refer to the package's specific release notes.
fix Consult the `permify-typescript` GitHub release notes or changelog for specific SDK-level breaking changes rather than solely relying on version number increments to determine client compatibility.
breaking The `permify-typescript` package specifies Node.js version 18 or higher as a minimum requirement. Running on older Node.js versions may lead to unexpected runtime errors, dependency conflicts, or installation failures.
fix Upgrade your Node.js environment to version 18 or newer. Consider using a Node.js version manager like `nvm` to easily switch between versions.
gotcha API requests require proper authentication. Failure to provide a valid `Authorization` header (e.g., a Bearer token) will result in API authentication failures, typically a 401 Unauthorized response from the Permify API.
fix Ensure the `Authorization` header is correctly set in the `Configuration` object, providing a valid Permify API key or token, preferably via environment variables (`process.env.PERMIFY_API_KEY`).
npm install permify-typescript
yarn add permify-typescript
pnpm add permify-typescript

This quickstart demonstrates how to initialize the Permify TypeScript client, configure it with a base path and an authorization header using environment variables, and then create a new tenant using the TenancyApi client.

import * as permify from "permify-typescript";

// Configure the client with your Permify API base path and authorization token.
// It's recommended to use environment variables for sensitive data.
const configuration = new permify.Configuration({
    basePath: process.env.PERMIFY_API_BASE_PATH ?? "http://localhost:3476",
    headers: {
        'Authorization': process.env.PERMIFY_API_KEY ? `Bearer ${process.env.PERMIFY_API_KEY}` : ""
    }
});

// Initialize the TenancyApi to interact with tenant-related endpoints.
const apiInstance = new permify.TenancyApi(configuration);

// Define a tenant ID, perhaps from a database or a unique identifier.
let tenantId = "example_tenant_id"; 

// Create a new tenant in Permify.
apiInstance.tenantsCreate({
    body: {
        id: tenantId,
        name: "Example Tenant"
    }
})
.then((data) => {
    console.log('API called successfully. Created tenant: ' + JSON.stringify(data.tenant));
})
.catch((error) => {
    console.error('Error creating tenant:', error.response?.data || error.message);
});