# acceptInviteWorkflow - Medusa Core Workflows Reference

This documentation provides a reference to the `acceptInviteWorkflow`. It belongs to the `@medusajs/medusa/core-flows` package.

This workflow accepts an invite and creates a user. It's used by the
[Accept Invite Admin API Route](https://docs.medusajs.com/api/admin#invites_postinvitesaccept).

The workflow throws an error if the specified token is not valid. Also, the workflow
requires an auth identity to be created previously. You can create an auth identity
using the [Retrieve Registration JWT Token API Route](https://docs.medusajs.com/api/admin#auth_postactor_typeauth_provider_register).

You can use this workflow within your customizations or your own custom workflows, allowing you to
accept invites within your custom flows.

[Source code](https://github.com/medusajs/medusa/blob/6695f211633620d5c8c54c8f7a866e92d4ec6166/packages/core/core-flows/src/invite/workflows/accept-invite.ts#L46)

## Examples

### API Route

```ts title="src/api/workflow/route.ts"
import type {
  MedusaRequest,
  MedusaResponse,
} from "@medusajs/framework/http"
import { acceptInviteWorkflow } from "@medusajs/medusa/core-flows"

export async function POST(
  req: MedusaRequest,
  res: MedusaResponse
) {
  const { result } = await acceptInviteWorkflow(req.scope)
    .run({
      input: {
        invite_token: "sk_123",
        auth_identity_id: "au_123",
        user: {
          email: "example@gmail.com",
          first_name: "John",
          last_name: "Doe",
        }
      }
    })

  res.send(result)
}
```

### Subscriber

```ts title="src/subscribers/order-placed.ts"
import {
  type SubscriberConfig,
  type SubscriberArgs,
} from "@medusajs/framework"
import { acceptInviteWorkflow } from "@medusajs/medusa/core-flows"

export default async function handleOrderPlaced({
  event: { data },
  container,
}: SubscriberArgs < { id: string } > ) {
  const { result } = await acceptInviteWorkflow(container)
    .run({
      input: {
        invite_token: "sk_123",
        auth_identity_id: "au_123",
        user: {
          email: "example@gmail.com",
          first_name: "John",
          last_name: "Doe",
        }
      }
    })

  console.log(result)
}

export const config: SubscriberConfig = {
  event: "order.placed",
}
```

### Scheduled Job

```ts title="src/jobs/message-daily.ts"
import { MedusaContainer } from "@medusajs/framework/types"
import { acceptInviteWorkflow } from "@medusajs/medusa/core-flows"

export default async function myCustomJob(
  container: MedusaContainer
) {
  const { result } = await acceptInviteWorkflow(container)
    .run({
      input: {
        invite_token: "sk_123",
        auth_identity_id: "au_123",
        user: {
          email: "example@gmail.com",
          first_name: "John",
          last_name: "Doe",
        }
      }
    })

  console.log(result)
}

export const config = {
  name: "run-once-a-day",
  schedule: "0 0 * * *",
}
```

### Another Workflow

```ts title="src/workflows/my-workflow.ts"
import { createWorkflow } from "@medusajs/framework/workflows-sdk"
import { acceptInviteWorkflow } from "@medusajs/medusa/core-flows"

const myWorkflow = createWorkflow(
  "my-workflow",
  () => {
    const result = acceptInviteWorkflow
      .runAsStep({
        input: {
          invite_token: "sk_123",
          auth_identity_id: "au_123",
          user: {
            email: "example@gmail.com",
            first_name: "John",
            last_name: "Doe",
          }
        }
      })
  }
)
```

## Steps

- [validateTokenStep](../../../Steps_Invite/functions/core_flows.Invite.Steps_Invite.validateTokenStep/page.mdx): This step validates a specified token and returns its associated invite.
  If not valid, the step throws an error.
- [createUsersWorkflow](../../../../User/Workflows_User/functions/core_flows.User.Workflows_User.createUsersWorkflow/page.mdx): Create one or more users with optional role assignment.
- [setAuthAppMetadataStep](../../../../Auth/Steps_Auth/functions/core_flows.Auth.Steps_Auth.setAuthAppMetadataStep/page.mdx): This step sets the \`app\_metadata\` property of an auth identity. This is useful to
  associate a user (whether it's an admin user or customer) with an auth identity
  that allows them to authenticate into Medusa.

  You can learn more about auth identites in
  \[this documentation]\(https://docs.medusajs.com/resources/commerce-modules/auth/auth-identity-and-actor-types).

  To use this for a custom actor type, check out \[this guide]\(https://docs.medusajs.com/resources/commerce-modules/auth/create-actor-type)
  that explains how to create a custom \`manager\` actor type and manage its users.
- [deleteInvitesStep](../../../Steps_Invite/functions/core_flows.Invite.Steps_Invite.deleteInvitesStep/page.mdx): This step deletes one or more invites.
- [emitEventStep](../../../../Common/Steps_Common/functions/core_flows.Common.Steps_Common.emitEventStep/page.mdx): This step emits an event, which you can listen to in a \[subscriber]\(https://docs.medusajs.com/learn/fundamentals/events-and-subscribers). You can pass data to the
  subscriber by including it in the \`data\` property.

  The event is only emitted after the workflow has finished successfully. So, even if it's executed in the middle of the workflow, it won't actually emit the event until the workflow has completed successfully.&#x20;
  If the workflow fails, it won't emit the event at all.

## Input

- AcceptInviteWorkflowInputDTO: (\[AcceptInviteWorkflowInputDTO]\(../../../../../types/WorkflowTypes/InviteWorkflow/interfaces/types.WorkflowTypes.InviteWorkflow.AcceptInviteWorkflowInputDTO/page.mdx))

  - invite\_token: (\`string\`) The invite token.

  - auth\_identity\_id: (\`string\`) The ID of the auth identity to associate the user with.

  - user: (\`object\`) The user to create.

    - email: (\`string\`) The email of the user.

    - first\_name: (\`null\` \\| \`string\`) The first name of the user.

    - last\_name: (\`null\` \\| \`string\`) The last name of the user.

    - avatar\_url: (\`null\` \\| \`string\`) The avatar URL of the user.

    - metadata: (\`null\` \\| \`Record\<string, unknown>\`) Custom key-value pairs of data to store in the user.

## Output

- UserDTO\[]: (\[UserDTO]\(../../../../../user/interfaces/user.UserDTO/page.mdx)\[])

  - id: (\`string\`) The ID of the user.

  - email: (\`string\`) The email of the user.

  - first\_name: (\`null\` \\| \`string\`) The first name of the user.

  - last\_name: (\`null\` \\| \`string\`) The last name of the user.

  - avatar\_url: (\`null\` \\| \`string\`) The avatar URL of the user.

  - metadata: (\`null\` \\| \`Record\<string, unknown>\`) Holds custom data in key-value pairs.

  - created\_at: (\`Date\`) The creation date of the user.

  - updated\_at: (\`Date\`) The updated date of the user.

  - deleted\_at: (\`null\` \\| \`Date\`) The deletion date of the user.

  - roles: (\`null\` \\| \`string\`\[]) The RBAC roles assigned to the user.

## Emitted Events

This section lists the events that are either triggered by the `emitEventStep` in the workflow, or by another workflow executed within this workflow.

:::note

The `emitEventStep` only emits the event after the workflow has finished successfully. So, even if it's executed in the middle of the workflow, it won't actually emit the event until the workflow has completed successfully. If the workflow fails, it won't emit the event at all.

:::

You can listen to these events in a subscriber, as explained in the [Subscribers](https://docs.medusajs.com/learn/fundamentals/events-and-subscribers) documentation.

|Event|Description|Payload|Action|
|---|---|---|---|
|\`user.created\`|Emitted when users are created.|\`\`\`ts
\{
&#x20; id, // The ID of the user
}
\`\`\`||
|\`invite.accepted\`|Emitted when an invite is accepted.|\`\`\`ts
\{
&#x20; id, // The ID of the invite
}
\`\`\`||


---

The best way to deploy Medusa is through Medusa Cloud where you get autoscaling production infrastructure fine tuned for Medusa. Create an account by signing up at cloud.medusajs.com/signup.
