<@U04UQ68083H> how can I update identity.default_s...
# ory-copilot
w
@chilly-king-10285 how can I update identity.default_schema_id of a project using go sdk?
c
A message from kapa.ai
To update the
default_schema_id
of a project using the Go SDK, you can use the
PatchIdentity
function provided by the Ory Go SDK. Here is an example of how you can do it:
Copy code
go package main      import (    "context"    "fmt"       client "<http://github.com/ory/client-go|github.com/ory/client-go>"   )      const (    YOUR_ORY_API_KEY        = "$YOUR_ORY_API_KEY"    YOUR_ORY_PROJECT_SLUG   = "$PROJECT_SLUG"    IDENTITY_ID_TO_UPDATE   = "$IDENTITY_ID"    NEW_SCHEMA_ID = "$NEW_SCHEMA_ID" )      func main() {    if err := updateDefaultSchemaID(NEW_SCHEMA_ID, IDENTITY_ID_TO_UPDATE); err != nil {     panic(err)    }   }      func updateDefaultSchemaID(newSchemaID, identityID string) error {    // Initialize the client configuration    cfg := client.NewConfiguration()    cfg.Servers = client.ServerConfigurations{     {      URL: fmt.Sprintf("<https://%s.projects.oryapis.com>", YOUR_ORY_PROJECT_SLUG),     },    }       // Create an instance of the API client    ory := client.NewAPIClient(cfg)       // Set the access token    ctx := context.Background()    ctx = context.WithValue(ctx, client.ContextAccessToken, YOUR_ORY_API_KEY)       // Prepare the JSON patch for the update    jsonPatch := []client.JsonPatch{     {Op: "replace", Path: "/default_schema_id", Value: newSchemaID},    }       // Apply the JSON patch to update the identity    _, _, err := ory.IdentityApi.PatchIdentity(ctx, identityID).JsonPatch(jsonPatch).Execute()    if err != nil {     return err    }       // Verify that the schema was updated    updatedIdentity, _, err := ory.IdentityApi.GetIdentity(ctx, identityID).Execute()    if err != nil {     return err    }       if updatedIdentity.SchemaId != newSchemaID {     return fmt.Errorf("default_schema_id wasn't updated")    }       fmt.Println("default_schema_id updated 🎉")       return nil   }
In this code, replace the constants
YOUR_ORY_API_KEY
,
YOUR_ORY_PROJECT_SLUG
,
IDENTITY_ID_TO_UPDATE
, and
NEW_SCHEMA_ID
with your actual values. This code will update the
default_schema_id
of the specified identity to the new schema ID. Please note that this code assumes that you have the necessary permissions to update the identity and that the new schema ID exists. For more information, you can refer to the [Ory documentation](https://www.ory.sh/docs/identities/model/manage-identity-schema#update-identities-to-use-a-new-schema).
w
@chilly-king-10285 I think I need to use ProjectAPI as:
Copy code
jsonPatch := []openapiclient.JsonPatch{*openapiclient.NewJsonPatch("replace", "/name")} // []JsonPatch |  (optional)

	configuration := openapiclient.NewConfiguration()
	apiClient := openapiclient.NewAPIClient(configuration)
	resp, r, err := apiClient.ProjectAPI.PatchProject(context.Background(), projectId).JsonPatch(jsonPatch).Execute()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error when calling `ProjectAPI.PatchProject``: %v\n", err)
		fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
	}
	// response from `PatchProject`: SuccessfulProjectUpdate
	fmt.Fprintf(os.Stdout, "Response from `ProjectAPI.PatchProject`: %v\n", resp)
how can I update identity schema with GO SDK?
c
A message from kapa.ai
To update the identity schema using the Go SDK, you can use the
PatchIdentity
function. Here is an example of how you can do it:
Copy code
go package main      import (    "context"    "fmt"       client "<http://github.com/ory/client-go|github.com/ory/client-go>"   )      const (    YOUR_ORY_API_KEY        = "$YOUR_ORY_API_KEY"    YOUR_ORY_PROJECT_SLUG   = "$PROJECT_SLUG"    IDENTITY_ID_TO_UPDATE   = "$IDENTITY_ID"    NEW_SCHEMA_ID = "$NEW_SCHEMA_ID" )      func main() {    if err := updateIdentitySchema(NEW_SCHEMA_ID, IDENTITY_ID_TO_UPDATE); err != nil {     panic(err)    }   }      func updateIdentitySchema(newSchemaID, identityID string) error {    // Initialize the client configuration    cfg := client.NewConfiguration()    cfg.Servers = client.ServerConfigurations{     {      URL: fmt.Sprintf("<https://%s.projects.oryapis.com>", YOUR_ORY_PROJECT_SLUG),     },    }       // Create an instance of the API client    ory := client.NewAPIClient(cfg)       // Set the access token    ctx := context.Background()    ctx = context.WithValue(ctx, client.ContextAccessToken, YOUR_ORY_API_KEY)       // Prepare the JSON patch for the update    jsonPatch := []client.JsonPatch{     {Op: "replace", Path: "/schema_id", Value: newSchemaID},    }       // Apply the JSON patch to update the identity    _, _, err := ory.IdentityApi.PatchIdentity(ctx, identityID).JsonPatch(jsonPatch).Execute()    if err != nil {     return err    }       // Verify that the schema was updated    updatedIdentity, _, err := ory.IdentityApi.GetIdentity(ctx, identityID).Execute()    if err != nil {     return err    }       if updatedIdentity.SchemaId != newSchemaID {     return fmt.Errorf("schema wasn't updated")    }       fmt.Println("Identity schema updated 🎉")       return nil   }
In this code, replace the constants
YOUR_ORY_API_KEY
,
YOUR_ORY_PROJECT_SLUG
,
IDENTITY_ID_TO_UPDATE
, and
NEW_SCHEMA_ID
with your actual values. This code will update the
schema_id
of the specified identity to the new schema ID. Please note that this code assumes that you have the necessary permissions to update the identity and that the new schema ID exists. For more information, you can refer to the [Ory documentation](https://www.ory.sh/docs/identities/model/manage-identity-schema#update-identities-to-use-a-new-schema).
w
@chilly-king-10285 I want to update
Copy code
"identity": {
            "default_schema_id": "<preset://username>",
in the project config using go SDK>?
c
A message from kapa.ai
I'm sorry, but based on the provided knowledge sources, there is no direct method available in the Ory Go SDK to update the
default_schema_id
in the project configuration. The Go SDK provides methods to interact with identities, such as creating, retrieving, updating, and deleting identities, but it does not provide a method to directly update the project configuration. However, you can use the Ory CLI to update the
default_schema_id
in the project configuration as shown in the following command:
Copy code
bash ory patch identity-config {your-project-id} \   --replace '/identity/default_schema_id="{unique-schema-id}"' \   --replace '/identity/schemas=[{"id":"{unique-schema-id}","url":"base64://'$schema'"}]'
In this command, replace
{your-project-id}
,
{unique-schema-id}
, and
$schema
with your actual values. This command will update the
default_schema_id
in the project configuration. For more information, you can refer to the [Ory documentation](https://www.ory.sh/docs/identities/model/manage-identity-schema#create-custom-schema).
w
@chilly-king-10285 how can I use go SDK to update project
default_schema_id
based on this example:
Copy code
projectId := "83fa99f0-ecc4-4faf-8131-7be47dd722f4"
	// jsonPatch := []openapiclient.JsonPatch{*openapiclient.NewJsonPatch("replace", "/name")} // []JsonPatch |  (optional)
	jsonPatch := []client.JsonPatch{
		{Op: "replace", Path: "/identity/default_schema_id", Value: "<preset://email>"},
	}

	resp, r, err := k.workspaceClient.ProjectAPI.PatchProject(context.Background(), projectId).JsonPatch(jsonPatch).Execute()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error when calling `ProjectAPI.PatchProject``: %v\n", err)
		fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
	}
c
A message from kapa.ai
New message text here
w
@chilly-king-10285 how can I update project Identity schema from preset://username to preset://email programatically?
c
A message from kapa.ai
To update the project identity schema programmatically, you can use the Ory CLI or SDKs. However, based on the provided knowledge sources, there is no direct method available in the Ory Go SDK to update the
default_schema_id
in the project configuration. Here is an example of how you can do it using the Ory CLI:
Copy code
bash # Encode your schema to Base64 and export it to a variable.   schema=$(cat {path-to-json-with-identity-schema} | base64)      # Update your project's configuration.   ory patch identity-config {your-project-id} \     --replace '/identity/default_schema_id="<preset://email>"' \     --replace '/identity/schemas=[{"id":"<preset://email>","url":"base64://'$schema'"}]'
In this command, replace
{path-to-json-with-identity-schema}
,
{your-project-id}
, and
$schema
with your actual values. This command will update the
default_schema_id
in the project configuration. For more information, you can refer to the [Ory documentation](https://www.ory.sh/docs/identities/model/manage-identity-schema#create-custom-schema).