careful-honey-27437
05/13/2025, 4:12 AMbrief-egg-61061
05/13/2025, 4:12 AMbrief-egg-61061
05/13/2025, 4:12 AMrelationships.json
) containing your relationships and then use the CLI to create them in bulk: 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: 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: 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!careful-honey-27437
05/13/2025, 4:12 AMbrief-egg-61061
05/13/2025, 4:12 AMbrief-egg-61061
05/13/2025, 4:13 AMCreateRelationship
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: 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).