Skip to main content

Update Relationship Tuples

This section will illustrate how to update relationship tuples.

Before You Start

  1. Deploy an instance of the OpenFGA server, and have ready the values for your setup: FGA_STORE_ID, FGA_API_URL and, if needed, FGA_API_TOKEN.
  2. You have installed the SDK.
  3. You have configured the authorization model.
  4. You have loaded FGA_STORE_ID and FGA_API_URL as environment variables.

Step By Step

Assume that you want to add user user:anne to have relationship reader with object document:Z

{
user: 'user:anne',
relation: 'reader',
object: 'document:Z',
}

01. Configure The OpenFGA API Client

Before calling the write API, you will need to configure the API client.

// import the SDK
const { OpenFgaClient } = require('@openfga/sdk');

// Initialize the SDK with no auth - see "How to setup SDK client" for more options
const fgaClient = new OpenFgaClient({
apiUrl: process.env.FGA_API_URL, // required, e.g. https://api.fga.example
storeId: process.env.FGA_STORE_ID,
authorizationModelId: process.env.FGA_MODEL_ID, // Optional, can be overridden per request
});

02. Calling Write API To Add New Relationship Tuples

To add the relationship tuples, we can invoke the write API.


await fgaClient.write({
writes: [
{"user":"user:anne","relation":"reader","object":"document:Z"}
],
}, {
authorization_model_id: "01HVMMBCMGZNT3SED4Z17ECXCA"
});

03. Calling Write API To Delete Relationship Tuples

To delete relationship tuples, we can invoke the write API.

Assume that you want to delete user user:anne's reader relationship with object document:Z

{
user: 'user:anne',
relation: 'reader',
object: 'document:Z',
}

await fgaClient.write({
deletes: [
{ user: 'user:anne', relation: 'reader', object: 'document:Z'}
],
}, {
authorization_model_id: "01HVMMBCMGZNT3SED4Z17ECXCA"
});
Managing User Access

Learn about how to give a user access to a particular object.

Managing Group Access

Learn about how to give a group of users access to a particular object.

Transactional Writes

Learn about how to update multiple relations within the same API call.