<@U04UQ68083H> Using the golang client, show an ex...
# ory-copilot
c
@chilly-king-10285 Using the golang client, show an example of how to use the Permissions API to grant the following: namespace: Article, object: article_abcdefg12345, relation: author, subject_id: 12345-5678-9087
b
A message from kapa.ai
Certainly! Below is a Go example using the Ory Permissions (Keto) Go SDK to grant the relation you specified: namespace
Article
, object
article_abcdefg12345
, relation
author
, and subject ID
<tel:12345-5678-9087|12345-5678-9087>
. This example demonstrates how to create the relationship using the REST API client.
Copy code
go package main  import (  "context"  "fmt"  "os"   ory "<http://github.com/ory/client-go|github.com/ory/client-go>" )  func main() {  // Set up the context with your Ory API Key  oryAuthedContext := context.WithValue(context.Background(), ory.ContextAccessToken, os.Getenv("ORY_API_KEY"))   // Define the relationship details  namespace := "Article"  object := "article_abcdefg12345"  relation := "author"  subjectId := "<tel:12345-5678-9087|12345-5678-9087>"   // Create the relationship payload  payload := ory.CreateRelationshipBody{   Namespace: &namespace,   Object:    &object,   Relation:  &relation,   SubjectId: &subjectId,  }   // Configure the Ory API client  configuration := ory.NewConfiguration()  configuration.Servers = []ory.ServerConfiguration{   {    URL: "https://<your-ory-project-url>", // Replace with your Ory project URL   },  }  client := ory.NewAPIClient(configuration)   // Create the relationship  _, r, err := client.RelationshipApi.CreateRelationship(oryAuthedContext).CreateRelationshipBody(payload).Execute()  if err != nil {   fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)   panic("Encountered error: " + err.Error())  }  fmt.Println("Successfully created relationship") }
Replace
https://<your-ory-project-url>
with your actual Ory project URL and ensure your
ORY_API_KEY
environment variable is set. This code is adapted from the official Ory documentation and demonstrates how to grant a permission by creating a relationship tuple using the Go SDK and the Permissions API [Ory Permissions Go SDK](https://www.ory.sh/docs/keto/sdk/go).