# Bootstrap a new OSDK TypeScript application with a service user As explained in the [permission types](../permissions/index.md#permission-types) section, the OSDK can be used to query data based on a service user's permissions rather than the end user's permissions. The following walkthrough shows how to use [Next.js© ↗](https://nextjs.org/) (external) to fetch data using the OSDK and a service user. When developing on a service or application that uses a confidential client, a service user will be created along with your Developer Console application. If you plan to create the application using an Ontology that belongs to an Organization separate from your default Organization, you must complete the steps to [share and enable the application](../how-to-create-backend-service-app-different-org/index.md). ## 1. Create an OSDK package using Developer Console Follow [Create a new Developer Console application](../create-application/index.md), using these backend service-specific selections: * On the **Application type** page, choose [**Backend service**](../permissions/index.md). ![The application type selection page with backend service selected.](../../../assets/c012e00d01dca2e884f2df0d289c0f0fc39fa0e63f46e067ba7a4f036b56c994.webp) * On the **Permissions** page, choose [**Application's permissions**](../permissions/index.md). ![The permission page shows the application permissions option selected.](../../../assets/2f8a27ed2a0cb222f47a02d9773ec37479ca893644fd0c66a458f57d1b0ab291.webp) Developer Console will create a service user for this application based on the application name. In the example above, the name of the generated service user is **Ontology SDK application using service user**. In addition to the [submission criteria](../../action-types/submission-criteria/index.md) for any action types, you must grant this service user the permissions required to [read the data of the object types](../../object-permissioning/overview/index.md) you will select in the next step. * On the **Resources** page, select **Yes, generate an Ontology SDK**. ![The resources page shows the option to generate an Ontology SDK.](../../../assets/08a7fc1599c814e3cc2296788523b79ada11db17b2fd86a1633cdf8484d61efc.webp) * Select an Ontology to use. :::callout{theme="warning"} Once the Ontology SDK has been generated, the Ontology associated with it cannot be changed. ::: * Select the object types and action types that you want the OSDK package to include. For this exercise, pick any object type available to you. ![Select the Ontology you want to use in the SDK and the specific object types or action types.](../../../assets/dca7a275861f9b6a68d297a09ed1dd1e873d731183dedad777bf252b352dc001.webp) Review and confirm the information you entered, then select **Create application** to see the client secret for the new application. Copy and store the secret securely as this is the only time it is visible. ![Copy the server side client secret that appears in the pop-up and securely store it.](../../../assets/651aa116ea54f60524e8842b07604953af437c1c99220319d86bfe6be0bc0d7a.webp) :::callout{theme="warning"} If you lose your client secret, you can rotate and obtain a new secret on the **OAuth & restrictions** page. Keep in mind that this will break existing applications using this service user and secret. ::: When you [package a confidential client application for Marketplace](../marketplace-installation/index.md#limitations), the client secret is not included. After installation, you must generate a new secret in the destination environment by navigating to **OAuth & restrictions** and selecting **Rotate secret**. Finally, select **Generate first version** to use your newly created OSDK. ![Select the Generate first version button to create your SDK.](../../../assets/4891b0b5cd02ced51a47070f23276f1ee625ebc01be50089b16cb7d970cca523.webp) ## 2. Install the generated SDK package Once the generation of the OSDK is complete, you will see a set of installation steps to guide you in installing the generated SDK in your code project. ## 3. Use the OSDK in your code project In this walkthrough, we use [Next.js© ↗](https://nextjs.org/). Next.js supports rendering code on the server side which is required for our service user example. To bootstrap a new Next.js project, follow the [Next.js© documentation ↗ ](https://nextjs.org/docs/getting-started/installation). ### Client and OAuth creation Service user authentication is done through a confidential OAuth client that allows you to access the Ontology with a client secret instead of requiring user authentication. Create a file named `client.ts` and enter the following code: ```typescript import { createConfidentialOauthClient } from "@osdk/oauth"; import { createClient } from "@osdk/client"; export const auth = createConfidentialOauthClient( process.env.CLIENT_ID!, process.env.CLIENT_SECRET!, process.env.STACK_URL!, ); export const client = createClient( process.env.STACK_URL!, , auth ) ``` Create a `.env` file with the same variables. **Do not check in this file to your code repository**. ```bash CLIENT_ID= CLIENT_SECRET= STACK_URL= # for example, https://myfoundrystack.com ``` ### Access the Ontology The following code uses a `Country` object type with a `@serverside-osdk-example/sdk` package name. Replace the example package name and object type with the package you created, and the object type you selected, respectively. Lastly, replace `{country.countryName}` with a property from your object type. Replace the code in the `page.tsx` with the following: ```typescript import { Country } from "@serverside-osdk-example/sdk"; import { client, auth } from "./client"; import { Osdk } from "@osdk/client"; async function getCountries(): Promise[]> { // Handle authentication await auth.signIn(); // You need to give the service user read access to the ontology try { const resp = await client(Country).fetchPage(); return resp.data; } catch (err) { console.log(err); } console.log("No countries found"); return []; } export default async function Home() { const countries: Osdk.Instance[] = await getCountries(); return (
{countries.map((country: Osdk.Instance) => ( {country.countryName} ))}
); } ``` To run a demo of your setup, first run the development server: ```bash npm run dev ``` Then, navigate to [http://localhost:3000 ↗](http://localhost:3000/) with your browser to view the result.