<@U04UQ68083H> Show me documentation about updatin...
# ory-copilot
b
@chilly-king-10285 Show me documentation about updating traits via the SDK.
b
A message from kapa.ai
Based on the knowledge sources, I can provide information about updating identity traits via the Ory SDK. Identity traits are the data associated with an identity that can be modified by the identity owner, such as username or email address. These traits can also be modified using the Ory SDK. ## Updating Identity Traits When updating identity traits, you need to use the Identity API. In SDK v1, the identity management APIs have moved from tag
v0alpha2
to
identity
. Here's how you can update identity traits using different SDK versions: ### Using SDK v1 (Current) For Go:
Copy code
go identity, res, err := client.   IdentityApi.UpdateIdentity(oryAuthedContext, toUpdate.Id).UpdateIdentityBody(ory.UpdateIdentityBody{     Traits: map[string]interface{}{       "email": "dev+not-" + x.NewUUID().String() + "@ory.sh",     },   }).Execute()
For JavaScript:
Copy code
javascript const identity = await ory   .identity.updateIdentity({     id: identity.id,     updateIdentityBody: {       schema_id: identity.schema_id,       traits: { email: "<mailto:user@example.com|user@example.com>", ...identity.traits },       state: identity.state,     }   })   .then(({ data }) => data)
[Upgrade guide](https://www.ory.sh/docs/guides/upgrade/sdk-v1#identity-management) provides more details on the SDK v1 upgrade for identity management. ### Important Considerations When updating traits, remember that: 1. The traits must be valid against the JSON Schema defined for the identity. 2. If you're updating the schema along with traits, you need to ensure compatibility. 3. When using webhooks to modify traits, the entire traits object is replaced, not just individual fields. For example, if you have an identity with an email trait and a webhook returns only a different trait, the email trait will be removed. Always ensure you return complete data:
Copy code
json {   "identity": {     "traits": {       "email": "<mailto:user@example.com|user@example.com>",       "another_value": "example"     }   } }
[Identity metadata & traits](https://www.ory.sh/docs/kratos/manage-identities/managing-users-identities-metadata#traits) provides more information about how traits work in Ory. You can also update identity traits through the Settings Flow, which allows users to update their own profile information. The
profile
method in the Settings Flow is specifically for updating an identity's traits.
b
@chilly-king-10285 How do I import/configure the IdentityAPI?
b
A message from kapa.ai
Based on the knowledge sources, I can show you how to import and configure the IdentityAPI in Ory. The IdentityAPI allows you to manage identities programmatically. ## Importing and Configuring IdentityAPI ### Using Go SDK
Copy code
go package main      import (       "context"       "fmt"       "os"          ory "<http://github.com/ory/client-go|github.com/ory/client-go>"   )      func main() {       // Initialize the client configuration     configuration := ory.NewConfiguration()       configuration.Servers = []ory.ServerConfiguration{           {               URL: "https://<your-ory-project-slug>.<http://projects.oryapis.com|projects.oryapis.com>", // Ory Network Project URL           },       }            // Create an instance of the API client     apiClient := ory.NewAPIClient(configuration)            // Use this context to access Ory APIs which require an Ory API Key     oryAuthedContext := context.WithValue(context.Background(), ory.ContextAccessToken, os.Getenv("ORY_API_KEY"))          // Now you can use the Identity API     // For example: apiClient.IdentityApi.CreateIdentity(...) }
For self-hosted Ory Kratos, you would use the Admin API URL:
Copy code
go configuration.Servers = []ory.ServerConfiguration{       {           URL: "<http://127.0.0.1:4434>", // Kratos Admin API       },   }
### Authentication The Identity API requires authentication with an API key: 1. Create a free Developer project with the [Ory CLI](https://www.ory.sh/docs/guides/cli/installation) or [Ory Console](https://console.ory.sh/guides) 2. Create a new [API key](https://www.ory.sh/docs/concepts/personal-access-token) and export it as an environment variable:
Copy code
export ORY_API_KEY=<your-api-key>
[Ory Identity Management Go SDK](https://www.ory.sh/docs/kratos/sdk/go#identity-api) provides more details on using the Identity API with Go. ### Alternative Configuration Method You can also set up the client like this:
Copy code
go package identity      import (       "context"       "fmt"       "os"          client "<http://github.com/ory/client-go|github.com/ory/client-go>"   )      var ory *client.APIClient 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)   }
Once configured, you can use the IdentityAPI to create, read, update, and delete identities, as well as manage their credentials, metadata, and state. Remember that all requests to the Identity API need to be authorized with your API key, as shown in the examples above. I hope this helps you get started with the IdentityAPI!
b
@chilly-king-10285 Typescript
b
A message from kapa.ai
Based on your previous questions about Ory SDKs, I understand you're looking for information on how to import and configure the IdentityAPI using TypeScript. Here's how to do it: ## Importing and Configuring IdentityAPI in TypeScript
Copy code
typescript import { Configuration, IdentityApi } from "@ory/client"  // Initialize the Identity API with your project's URL const identity = new IdentityApi(   new Configuration({     basePath: `https://${process.env.ORY_PROJECT_SLUG}.<http://projects.oryapis.com|projects.oryapis.com>`,     accessToken: `${process.env.ORY_API_KEY}`, // Required for admin operations     baseOptions: {       withCredentials: true, // Important for CORS       timeout: 30000, // 30 seconds     },   }), )
### For SDK v1 (Current Version) In SDK v1, the APIs are organized by domain rather than version. For identity management, you'll use the
IdentityApi
class:
Copy code
typescript import {   Configuration,   IdentityApi,   FrontendApi,   OAuth2Api, } from "@ory/client"  const config = new Configuration({   basePath: "<https://your-project-slug.projects.oryapis.com/>",   accessToken: process.env.ORY_API_KEY,   baseOptions: {     withCredentials: true, // Important for CORS     timeout: 30000, // 30 seconds   }, })  const ory = {   identity: new IdentityApi(config),   frontend: new FrontendApi(config),   oauth2: new OAuth2Api(config), }
### Example: Listing Sessions
Copy code
typescript export async function listSessions(   expandOptions?: Array<"Devices" | "Identity">,   pageToken?: string,   pageSize?: number,   active?: boolean, ) {   // All parameters here are optional   return await identity.listSessions({     expand: expandOptions,     active: active, // Optional parameter to filter sessions based on state     pageToken: pageToken,     pageSize: pageSize, // Optional parameter to control the number of sessions per page   }) }
### Example: Getting a Session
Copy code
typescript export async function getSession(   sessionId: string,   expandOptions?: Array<"Devices" | "Identity">, ) {   return await identity.getSession({     id: sessionId,     expand: expandOptions,   }) }
When using the SDK in a production environment with a custom domain, you would configure it like this:
Copy code
typescript import { Configuration, FrontendApi } from "@ory/client-fetch"  const basePath = "<https://ory.your-custom-domain.com>"  const ory = new FrontendApi(   new Configuration({     basePath,     credentials: "include",   }), )
Remember that for administrative operations (like managing identities), you need to include your API key in the configuration. [Ory API Reference](https://www.ory.sh/docs/reference/api) provides complete documentation for all available APIs. Hope this helps with your TypeScript implementation!