> ## Documentation Index
> Fetch the complete documentation index at: https://docs.autoblocks.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Quick Start

> Get started with the Autoblocks TypeScript Prompt SDK to manage and execute your prompts with type safety and autocomplete.

# TypeScript Prompt SDK Quick Start

## Install

<CodeGroup>
  ```bash npm
  npm install @autoblocks/client
  ```

  ```bash yarn
  yarn add @autoblocks/client
  ```

  ```bash pnpm
  pnpm add @autoblocks/client
  ```
</CodeGroup>

## Create a Prompt App

Before creating prompts, you need to create a prompt app. Apps are the top-level organizational unit in Autoblocks and help you manage access and track usage of your prompts.

1. Go to the [apps page](https://app-v2.autoblocks.ai/apps)
2. Click "Create App"
3. Select "Prompt App" as the app type
4. Give your app a name and description
5. Configure access settings for your team members

<Note>
  All prompts you create will be associated with this app. Make sure to choose a name that reflects the purpose of your prompts (e.g., "Content Generation" or "Customer Support").
</Note>

## Generate types

In order to generate types, you need to set your Autoblocks API key from the [settings](https://app-v2.autoblocks.ai/settings/api-keys) page
as an environment variable:

```bash
export AUTOBLOCKS_V2_API_KEY=...
```

Then, add the `prompts generate-v2` command to your `package.json` scripts:

```json
"scripts": {
  "gen": "prompts generate-v2"
}
```

You will need to run this script any time you deploy a new major version of your prompt.

<Note>
  Make sure to generate the types in your CI/CD pipeline before running type checking on your application.

  ```json
  "scripts": {
    "gen": "prompts generate-v2",
    "type-check": "npm run gen && tsc --noEmit"
  }
  ```
</Note>

## Initialize the prompt manager

Create a single instance of the prompt manager for the lifetime of your application.
When initializing the prompt manager, the major version must be pinned while the minor version can either be
pinned or set to `'latest'`:

<CodeGroup>
  ```ts pinned
  import { AutoblocksPromptManagerV2 } from '@autoblocks/client/prompts';

  const mgr = new AutoblocksPromptManagerV2({
    appName: 'my-app',
    id: 'text-summarization',
    version: {
      major: '1',
      minor: '0',
    },
  });
  ```

  ```ts latest
  import { AutoblocksPromptManagerV2 } from '@autoblocks/client/prompts';

  const mgr = new AutoblocksPromptManagerV2({
    appName: 'my-app',
    id: 'text-summarization',
    version: {
      major: '1',
      minor: 'latest',
    },
  });
  ```
</CodeGroup>

<Note>
  When the version is set to `'latest'`, the prompt manager periodically refreshes the in-memory prompt
  in the background according to the `refreshInterval`.
  See the [`AutoblocksPromptManager`](/v2/guides/prompt-management/typescript/sdk-reference#autoblocks-prompt-manager) reference for more information.
</Note>

## Wait for the manager to be ready

At the entrypoint to your application, wait for the prompt manager to be ready before handling requests.

```ts
await mgr.init();
```

## Execute a prompt

The `exec` method on the prompt manager starts a new prompt execution context.
It creates a [`PromptExecutionContext`](/v2/guides/prompt-management/typescript/sdk-reference#prompt-execution-context)
instance that gives you fully-typed access to the prompt's templates and parameters:

```ts
const response = await mgr.exec(async ({ prompt }) => {
  const params: ChatCompletionCreateParamsNonStreaming = {
    model: prompt.params.model,
    temperature: prompt.params.temperature,
    messages: [
      {
        role: 'system',
        content: prompt.renderTemplate({
          template: 'system',
          params: {
            languageRequirement: prompt.renderTemplate({
              template: 'util/language',
              params: {
                language: 'Spanish',
              },
            }),
            toneRequirement: prompt.renderTemplate({
              template: 'util/tone',
              params: {
                tone: 'silly',
              },
            }),
          },
        }),
      },
      {
        role: 'user',
        content: prompt.renderTemplate({
          template: 'user',
          params: {
            document: 'mock document',
          },
        }),
      },
    ],
  };

  const response = await openai.chat.completions.create(params);

  return response;
});
```

## Develop locally against a prompt revision that hasn't been deployed

As you create new revisions in the UI, your private revisions (or revisions that have been shared by your teammates)
can be pulled down using `dangerously-use-undeployed`:

<CodeGroup>
  ```ts latest
  import { AutoblocksPromptManagerV2 } from '@autoblocks/client/prompts';

  const mgr = new AutoblocksPromptManagerV2({
    appName: 'my-app',
    id: 'text-summarization',
    version: {
      major: 'dangerously-use-undeployed',
      minor: 'latest',
    },
  });
  ```

  ```ts pinned
  import { AutoblocksPromptManagerV2 } from '@autoblocks/client/prompts';

  const mgr = new AutoblocksPromptManagerV2({
    appName: 'my-app',
    id: 'text-summarization',
    version: {
      major: 'dangerously-use-undeployed',
      minor: 'clvods6wq0003m44zc8sizv2l',
    },
  });
  ```
</CodeGroup>

<Note>
  As the name suggests, this should only be used in local development and never in production.
</Note>

## Organizing multiple prompt managers

If you are using many prompt managers, we recommend initializing them in a single file and importing them as a module:

`prompts.ts`:

```typescript
import { AutoblocksPromptManagerV2 } from '@autoblocks/client/prompts';

const refreshInterval = { seconds: 5 };

const managers = {
  textSummarization: new AutoblocksPromptManagerV2({
    appName: 'my-app',
    id: 'text-summarization',
    version: {
      major: '1',
      minor: 'latest',
    },
    refreshInterval,
  }),
  flashcardGenerator: new AutoblocksPromptManagerV2({
    appName: 'my-app',
    id: 'flashcard-generator',
    version: {
      major: '1',
      minor: 'latest',
    },
    refreshInterval,
  }),
  studyGuideOutline: new AutoblocksPromptManagerV2({
    appName: 'my-app',
    id: 'study-guide-outline',
    version: {
      major: '1',
      minor: 'latest',
    },
    refreshInterval,
  }),
};

async function init() {
  await Promise.all(Object.values(managers).map(mgr => mgr.init()));
}

export default {
  init,
  ...managers,
};
```

Make sure to call `init` at the entrypoint of your application:

```typescript
import prompts from '~/prompts';

async function start() {
  await prompts.init();

  ...
}
```

Then, throughout your application, import the entire `prompts`
module and use the prompt managers as needed:

```typescript
import prompts from '~/prompts';

prompts.textSummarization.exec(({ prompt }) => {
  ...
});

prompts.flashcardGenerator.exec(({ prompt }) => {
  ...
});

prompts.studyGuideOutline.exec(({ prompt }) => {
  ...
});
```
