<@U04UQ68083H> How I can make toSession request wi...
# ory-copilot
p
@chilly-king-10285 How I can make toSession request with JWT token in Go
b
A message from kapa.ai
To make a
toSession
request with a JWT token in Go, you can use the
XSessionToken
method of the
FrontendApi
in the Ory Go SDK. Here's an example:
Copy code
go package frontend      import (    "context"    "fmt"    "os"       "<http://github.com/ory/client-go|github.com/ory/client-go>"   )      type oryMiddleware struct {    ory *ory.APIClient   }      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 CheckSession(ctx context.Context, sessionToken string) (session *client.Session, err error) {    session, _, err = ory.FrontendApi.ToSession(ctx).     XSessionToken(sessionToken).     Execute()    if err != nil {     // error revoking the session, for example due to expired token provided     return nil, err    }       return session, nil   }
In this example,
sessionToken
is the JWT token you want to use for the request. The
CheckSession
function will return the session if the token is valid, or an error if the token is invalid or expired. Please note that you need to replace
ORY_PROJECT_SLUG
with your actual project slug. Source: [Ory Documentation](https://www.ory.sh/docs/kratos/sdk/go#native---check-who-the-current-http-session-belongs-to)
p
for self-hosted