<@U04UQ68083H> Show an example of how to create re...
# ory-copilot
c
@chilly-king-10285 Show an example of how to create relationships in bulk using the Ory Permissions API
b
A message from kapa.ai
To create relationships in bulk using the Ory Permissions API, you can use the Ory CLI or the REST API. Here are two approaches based on the knowledge sources: --- ### 1. Using the Ory CLI You can prepare a JSON file (e.g.,
relationships.json
) containing your relationships and then use the CLI to create them in bulk:
Copy code
bash ory create relationships relationships.json
This command will traverse the JSON file and create all the relationships defined within it. You can also pass
-
to read from STDIN. Example output:
Copy code
NAMESPACE   OBJECT                RELATION NAME   SUBJECT Group       developer             members         patrik Group       developer             members         User:Patrik Group       developer             members         User:Henning Folder      keto/                 viewers         Group:developer#members File        keto/README.md        parents         Folder:keto/ Folder      keto/src/             parents         Folder:keto/ File        keto/src/main.go      parents         Folder:keto/src/ File        private               owners          User:Henning
[Bulk creation with CLI](https://www.ory.sh/docs/guides/permissions/overview/#example) | [CLI command reference](https://www.ory.sh/docs/cli/ory-create-relationships/#ory-create-relationships) --- ### 2. Using the REST API While the REST API primarily supports creating relationships one at a time via the
PUT /admin/relation-tuples
endpoint, you can automate bulk creation by sending multiple requests in a script or programmatically. Here’s an example of the request body for a single relationship:
Copy code
json {   "namespace": "Blog",   "object": "secret_post",   "relation": "view",   "subject_id": "Bob" }
[REST API reference](https://www.ory.sh/docs/reference/api#tag/relationship) For true bulk operations, the CLI is the recommended approach as it is designed to handle multiple relationships efficiently. --- Summary: - For bulk creation, use the Ory CLI with a JSON file containing your relationships. - The REST API supports single relationship creation per request, so bulk operations require scripting multiple calls. If you need a code example for scripting bulk creation via the REST API, let me know!
c
@chilly-king-10285 use the go client sdk
b
A message from kapa.ai
To create relationships in bulk using the Ory Permissions Go SDK, the recommended approach is to use the Ory CLI with a JSON file for true bulk operations. However, if you want to use the Go SDK, you will need to iterate over your relationships and call the
CreateRelationship
method for each one, as the SDK and API do not provide a single bulk endpoint for relationship creation. Here’s an example of how you can do this with the Go SDK, based on the official documentation:
Copy code
go package main  import (  "context"  "fmt"  "os"   ory "<http://github.com/ory/client-go|github.com/ory/client-go>" )  var oryAuthedContext = context.WithValue(context.Background(), ory.ContextAccessToken, os.Getenv("ORY_API_KEY"))  func main() {  configuration := ory.NewConfiguration()  configuration.Servers = []ory.ServerConfiguration{   {    URL: "<https://your-ory-project-url>", // Replace with your Ory project URL   },  }  client := ory.NewAPIClient(configuration)   relationships := []ory.CreateRelationshipBody{   {    Namespace: ory.PtrString("Group"),    Object:    ory.PtrString("developer"),    Relation:  ory.PtrString("members"),    SubjectId: ory.PtrString("patrik"),   },   {    Namespace: ory.PtrString("Group"),    Object:    ory.PtrString("developer"),    Relation:  ory.PtrString("members"),    SubjectId: ory.PtrString("User:Henning"),   },   // Add more relationships as needed  }   for _, rel := range relationships {   _, r, err := client.RelationshipApi.CreateRelationship(oryAuthedContext).CreateRelationshipBody(rel).Execute()   if err != nil {    fmt.Fprintf(os.Stderr, "Failed to create relationship: %v\nFull HTTP response: %v\n", err, r)    continue   }   fmt.Println("Successfully created tuple:", *rel.Namespace, *rel.Object, *rel.Relation, *rel.SubjectId)  } }
This code loops through a slice of relationship definitions and creates each one using the Go SDK. There is currently no single bulk API call for relationships in the Go SDK or REST API, so this is the standard approach for programmatic bulk creation [Ory Permissions Go SDK](https://www.ory.sh/docs/keto/sdk/go/). If you need true bulk creation (e.g., from a JSON file), the Ory CLI is the recommended tool [ory create relationships](https://www.ory.sh/docs/cli/ory-create-relationships/#ory-create-relationships).