<@U04UQ68083H> Show the current best practice for ...
# ory-copilot
c
@chilly-king-10285 Show the current best practice for creating a new relationship via the Ory Permissions API in node.js using the client-fetch module as a tenant of Ory Network.
b
A message from kapa.ai
The current best practice for creating a new relationship via the Ory Permissions API in Node.js, as a tenant of Ory Network, is to use the @ory/client-fetch module and the relationship.createRelationship method. This approach is based on the latest SDK structure, where relationship management is handled in the relationship module. Here’s a concise example:
Copy code
js import { RelationshipApi, Configuration } from "@ory/client-fetch"  const ory = new RelationshipApi(   new Configuration({     basePath: "https://<your-project-slug>.<http://projects.oryapis.com|projects.oryapis.com>", // or your tunnel/custom domain URL     accessToken: "<your-ory-access-token>", // Use a valid Ory PAT or token   }) )  const createRelationshipBody = {   namespace: "Group",   object: "developer",   relation: "members",   subject: "User:Patrik" }  ory.createRelationship({ createRelationshipBody })   .then(response => {     console.log("Relationship created:", response.data)   })   .catch(error => {     console.error("Error creating relationship:", error)   })
Key points: - Use the relationship.createRelationship method, not the deprecated write.createRelationTuple. - Provide your Ory Network project’s basePath and a valid access token for authentication. - The body should specify the namespace, object, relation, and subject as per your permission model. This pattern is confirmed in the official Ory SDK v1 upgrade guide and aligns with the current API structure for Ory Network tenants [SDK V1 upgrade guide](https://www.ory.sh/docs/guides/upgrade/sdk-v1#permissions--access-control). For more details on relationship concepts and API authentication, see the Ory documentation on [relationships](https://www.ory.sh/docs/keto/concepts/relation-tuples) and [API reference](https://www.ory.sh/docs/reference/api).
c
@chilly-king-10285 { “response”: {}, “name”: “ResponseError” }
b
A message from kapa.ai
The knowledge sources provide details on the Ory Permissions API (relationship API) and its expected responses, but do not include a full, working Node.js example using @ory/client-fetch. However, they do specify the API endpoint, required fields, and response structure for creating a relationship. Best Practice (Based on Knowledge Sources): 1. Endpoint & Method: Use the
createRelationship
method of the Ory Permissions API. The request body must include at least
namespace
,
object
, and
relation
, and either
subject_id
or a
subject_set
object. 2. Request Body Example:
Copy code
json    {      "namespace": "example_namespace",      "object": "example_object",      "relation": "example_relation",      "subject_id": "example_subject"    }
Or, for a subject set:
Copy code
json    {      "namespace": "example_namespace",      "object": "example_object",      "relation": "example_relation",      "subject_set": {        "namespace": "subject_namespace",        "object": "subject_object",        "relation": "subject_relation"      }    }
3. Response: - On success: HTTP 201 with the created relationship object. - On error: Standard Ory JSON API error format with details in the
error
property. 4. Error Handling: If you receive a response like:
Copy code
json    {      "response": {},      "name": "ResponseError"    }
This typically means the API returned an error response (possibly with an empty body), and the client library wrapped it in a
ResponseError
. Check your request body for required fields and ensure your access token and base path are correct. References: - [Ory API Reference – createRelationship](https://www.ory.sh/docs/reference/api) - [Keto API Reference – createRelationship](https://www.ory.sh/docs/keto/reference/rest-api) Summary: - Ensure your request body includes all required fields. - Use a valid access token and correct base path for your Ory Network project. - Handle errors by inspecting the
ResponseError
and checking for missing or invalid fields in your request. If you need a specific code example, the knowledge sources do not provide one, but the above structure and error handling are directly derived from the official API documentation. If you need further troubleshooting, please provide the exact request payload and configuration you are using.
c
Debug this: async function createKetoRelationshipForSubject(subject, createRelationshipBody) { try { await keto.createRelationship({createRelationshipBody}) // log(
[Ory Keto] Created: ${tuple.namespace} ${tuple.object} ${tuple.relation} ${tuple.subject_id}
) } catch (err) { console.log(JSON.stringify(err, null, 3)) log(“[Ory Keto] Error creating relationship: ” + err) throw new Error(
[Ory Keto] Failed to create sysadmin relationship
) } }
@chilly-king-10285 Debug this: async function createKetoRelationshipForSubject(subject, createRelationshipBody) { try { await keto.createRelationship({createRelationshipBody}) // log(
[Ory Keto] Created: ${tuple.namespace} ${tuple.object} ${tuple.relation} ${tuple.subject_id}
) } catch (err) { console.log(JSON.stringify(err, null, 3)) log(“[Ory Keto] Error creating relationship: ” + err) throw new Error(
[Ory Keto] Failed to create sysadmin relationship
) } }
b
A message from kapa.ai
Based on the knowledge sources, here are the key points to debug your function: 1. Request Body Structure: The request body for
createRelationship
must include at least
namespace
,
object
, and
relation
, and either
subject_id
or
subject_set
. The property should be named exactly as required by the API (
subject_id
, not
subjectId
or
subject
). Double-check that your
createRelationshipBody
matches this structure:
Copy code
json    {      "namespace": "example_namespace",      "object": "example_object",      "relation": "example_relation",      "subject_id": "example_subject"    }
or, for a subject set:
Copy code
json    {      "namespace": "example_namespace",      "object": "example_object",      "relation": "example_relation",      "subject_set": {        "namespace": "subject_namespace",        "object": "subject_object",        "relation": "subject_relation"      }    }
[Keto API Reference – createRelationship](https://www.ory.sh/docs/keto/reference/rest-api) 2. Error Handling: The error you posted:
Copy code
json    {      "response": {},      "name": "ResponseError"    }
suggests that the API call failed, but the response body was empty. This often happens if the request is malformed or required fields are missing. The Ory API returns detailed error messages in the
error
property of the response, but if the body is empty, it may indicate a client-side or network issue, or a misconfiguration in the request. 3. Access Token and Base Path: Ensure you are passing a valid Ory Network access token and the correct base path for your project, as required for authentication [Ory API Reference – Authentication](https://www.ory.sh/docs/reference/api). 4. Function Parameter: Your function signature includes
subject
, but you are not using it. Make sure you are passing the correct
createRelationshipBody
and that it contains the required fields. 5. Logging: The commented-out log line references
tuple
, which is not defined in your function. If you want to log the created relationship, you should capture the response from
createRelationship
and log its properties. Example Correction:
Copy code
js async function createKetoRelationshipForSubject(createRelationshipBody) {   try {     const response = await keto.createRelationship({ createRelationshipBody })     // If you want to log the created relationship:     // const tuple = response.data     // log(`[Ory Keto] Created: ${tuple.namespace} ${tuple.object} ${tuple.relation} ${tuple.subject_id}`)   } catch (err) {     console.log(JSON.stringify(err, null, 3))     log("[Ory Keto] Error creating relationship: " + err)     throw new Error("[Ory Keto] Failed to create sysadmin relationship")   } }
Summary: - Double-check the structure and field names in your
createRelationshipBody
. - Ensure authentication and base path are correct. - Capture and log the response for better debugging. - If the error persists and the response is empty, verify network connectivity and that your API client is up to date. If you need more specific troubleshooting, please provide the actual
createRelationshipBody
you are sending and details about your
keto
client configuration. The knowledge sources do not provide further details on Node.js error handling or SDK-specific quirks beyond the API contract and error format.