<@U04UQ68083H> how to use frontend.toSession on AP...
# ory-copilot
m
@chilly-king-10285 how to use frontend.toSession on API
c
A message from kapa.ai
The
frontend.toSession
method is used to check if a session is active. It is part of the Ory SDK and can be used in different programming languages like Go, JavaScript, and TypeScript. Here are some examples of how to use it: Go:
Copy code
go package main      import (    "context"    "fmt"    "os"       client "<http://github.com/ory/kratos-client-go|github.com/ory/kratos-client-go>"   )      func main() {    configuration := client.NewConfiguration()    configuration.Servers = []client.ServerConfiguration{    {    URL: "<http://127.0.0.1:4433>", // Kratos Public API    },    }    apiClient := client.NewAPIClient(configuration)    cookie := "ory_kratos_session=MTY0ODgyMTExN3xEdi1CQkFFQ180SUFBUkFCRUFBQVJfLUNBQUVHYzNSeWFXNW5EQThBRFhObGMzTnBiMjVmZEc5clpXNEdjM1J5YVc1bkRDSUFJRkZDVFVKbFNIcEJOalZyY0Vad1JEZ3dNMng1V0RsWlpEQlFXa3RoUjNJenzKCLhzCkox1OmvNJlKcqtWuNkSnPLrUgM6Ew2EMYksfg=="    resp, r, err := apiClient.FrontendApi.ToSession(context.Background()).Cookie(cookie).Execute()    if err != nil {    fmt.Fprintf(os.Stderr, "Error when calling `FrontendApi.ToSession``: %v\n", err)    fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)    }    // response from `ToSession`: Session    fmt.Fprintf(os.Stdout, "Traits %v\n", resp.Identity.Traits)   }
(Source: [Ory Documentation](https://www.ory.sh/docs/kratos/self-hosted/go#use-frontend-api)) JavaScript:
Copy code
javascript import { Configuration, FrontendApi } from "@ory/client"      const frontend = new FrontendApi(    new Configuration({    basePath: `https://${process.env.ORY_PROJECT_SLUG}.<http://projects.oryapis.com|projects.oryapis.com>`,    }),   )      export async function checkSession(sessionId: string, token: string) {    return await frontend.toSession({    xSessionToken: token,    })   }
(Source: [Ory Documentation](https://www.ory.sh/docs/kratos/bring-your-own-ui/custom-ui-basic-integration#session-checks)) In both examples, the
toSession
method is used to check if a session is active. The method requires a session token or a cookie that represents the session. The session token or cookie can be obtained when a user logs in. If the session is active, the method will return the session details.