square-napkin-92357
07/11/2025, 9:10 AMbrief-egg-61061
07/11/2025, 9:10 AMbrief-egg-61061
07/11/2025, 9:10 AMgo package identity import ( "context" "fmt" "os" client "<http://github.com/ory/client-go|github.com/ory/client-go>" ) var authed = context.WithValue(context.Background(), client.ContextAccessToken, os.Getenv("ORY_API_KEY")) func init() { cfg := client.NewConfiguration() cfg.Servers = client.ServerConfigurations{ {URL: fmt.Sprintf("<https://%s.projects.oryapis.com>", os.Getenv("ORY_PROJECT_SLUG"))}, } ory = client.NewAPIClient(cfg) } func setState(identityId string, state string) (err error) { _, _, err = ory.IdentityApi. PatchIdentity(authed, identityId). JsonPatch([]client.JsonPatch{{Op: "replace", Path: "/state", Value: state}}).Execute() return err }
This code replaces the state of the identity (e.g., "active" or "inactive") using a JSON Patch operation. You can adapt the JsonPatch
array to update other fields as needed, such as /schema_id
or traits. [See more in the official docs](https://www.ory.sh/docs/kratos/sdk/go#patch-identity). ### 2. Update Identity (Full Update) If you want to overwrite the entire identity (except credentials), use the UpdateIdentity
method. Here’s a code snippet: go identity, res, err := client. IdentityApi.UpdateIdentity(oryAuthedContext, toUpdate.Id). UpdateIdentityBody(client.UpdateIdentityBody{ SchemaId: "default", Traits: map[string]interface{}{ "email": "<mailto:new@email.com|new@email.com>", // ...other traits }, State: "active", }).Execute()
This will update the identity’s schema, traits, and state. [See the SDK upgrade guide for more details](https://www.ory.sh/docs/guides/upgrade/sdk-v1#identity-management). ### Notes - Always authenticate your requests using an Ory API key. - For partial updates, prefer PatchIdentity
to avoid overwriting unintended fields. - For full updates, use UpdateIdentity
and provide the full payload (except credentials). If you need to update the identity schema, you can use a JSON Patch as shown above, or use the full update method. [See the schema migration example](https://www.ory.sh/docs/identities/model/manage-identity-schema#update-identities-to-use-a-new-schema). Let me know if you need a more specific example!