<@U04UQ68083H> how to invoke admin api with powers...
# ory-copilot
b
@chilly-king-10285 how to invoke admin api with powershell in ory network
c
A message from kapa.ai
To invoke the Admin API with PowerShell in the Ory Network, you need to set up your environment variables and use the Invoke-RestMethod cmdlet to send a request to the API. First, set up your environment variable for the Ory SDK URL:
Copy code
powershell $Env:ORY_SDK_URL = "https://$<http://PROJECT_SLUG.projects.oryapis.com|PROJECT_SLUG.projects.oryapis.com>"
Replace
$PROJECT_SLUG
with your project's slug. Then, use the Invoke-RestMethod cmdlet to send a request to the API. For example, to create a new user account, you can use the following command:
Copy code
powershell Invoke-RestMethod -Uri "$Env:ORY_SDK_URL/admin/identities" -Method Post -Headers @{"Authorization" = "Bearer $API_KEY"; "Content-Type" = "application/json"} -Body '{"schema_id": "<preset://email>", "traits": { "email": "$USER_EMAIL_ADDRESS" }}'
Replace
$API_KEY
with your API key and
$USER_EMAIL_ADDRESS
with the email address of the user you want to create. Remember, you need an API Key to call these endpoints. You can create API Keys in the Ory Console by following these steps: 1. Go to Project settingsAPI Keys in the [Ory Console](https://console.ory.sh/projects/current/developers). 2. Click Create new API key 3. Enter the API Key name and click Create 4. Copy the created API Key The API Key you created is displayed only once. When you close the prompt, you can't access the key again. Make sure to save the API Key before you close the prompt or reload the page. Sources: - [Setting up environment variables](https://www.ory.sh/docs/getting-started/integrate-auth/expressjs#run-your-expressjs-app) - [Creating a new user account](https://www.ory.sh/docs/kratos/manage-identities/invite-users#procedure) - [Creating API Keys](https://www.ory.sh/docs/concepts/personal-access-token#authorizing-ory-network-project-admin-apis)
b
@chilly-king-10285 how to query list of all identities
c
A message from kapa.ai
To query a list of all identities, you can use the Ory or Kratos API or CLI. Using the API: You can use the
GET /admin/identities
endpoint to list all identities in the system. Here is an example of how to use this endpoint with curl:
Copy code
bash curl -X GET "<https://your-ory-instance.com/admin/identities>"
Replace
<https://your-ory-instance.com>
with the URL of your Ory instance. Using the CLI: You can use the
ory list identities
or
kratos list identities
command to list all identities. Here is an example of how to use this command:
Copy code
bash ory list identities --page-size 100 --consistency eventual
or
Copy code
bash kratos list identities --page-size 100 --consistency eventual
This command will return a list of identities. The
--page-size
option specifies the maximum number of items to return, and the
--consistency
option specifies the read consistency to use. Sources: - [Ory API Reference](https://www.ory.sh/docs/reference/api) - [Kratos API Reference](https://www.ory.sh/docs/kratos/reference/api) - [Ory CLI Documentation](https://www.ory.sh/docs/cli/ory-list-identities#ory-list-identities) - [Kratos CLI Documentation](https://www.ory.sh/docs/kratos/cli/kratos-list-identities#kratos-list-identities)
b
@chilly-king-10285 when using the Ory SDK in Next.JS, how do I detect when the user's session is no longer active?
c
A message from kapa.ai
In Next.js, you can use the Ory SDK to check if a user's session is active by calling the
toSession()
method. If the user's session is no longer active, the
toSession()
method will throw an error, which you can catch and handle accordingly. Here is an example of how you can do this:
Copy code
jsx import { Configuration, FrontendApi, Session } from "@ory/client" import { useEffect, useState } from "react" import { useRouter } from "next/router"  const ory = new FrontendApi(new Configuration(edgeConfig))  export default function Home() {   const router = useRouter()   const [session, setSession] = useState<Session | undefined>()    useEffect(() => {     ory       .toSession()       .then(({ data }) => {         // User has a session!         setSession(data)       })       .catch(() => {         // Redirect to login page         return router.push(edgeConfig.basePath + "/ui/login")       })   }, [router])    if (!session) {     // Still loading     return null   }    // Rest of your component }
In this example, the
useEffect
hook is used to call the
toSession()
method when the component mounts. If the user has an active session, the session data is stored in the
session
state variable. If the user does not have an active session, the user is redirected to the login page. Please note that you need to replace
edgeConfig
with your actual configuration. Source: [Getting-started > Integrate-auth > Integrate authentication into Next.js > Create Next.js app > Require login to access the home page Part 1](https://www.ory.sh/docs/getting-started/integrate-auth/nextjs#require-login-to-access-the-home-page)