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

# Integrar con Bun

> Agrega cobros y pagos a tu aplicación de Bun.

Instala la Commet Skill para que tu agente integre el SDK actual y valide su trabajo contra el contrato vivo de la API.

```bash theme={null}
npx skills add commet-labs/skills --skill commet
```

<Steps>
  <Step title="Instalar">
    ```bash theme={null}
    bun add @commet/node
    ```
  </Step>

  <Step title="Configurar">
    ```bash title=".env" theme={null}
    COMMET_API_KEY=ck_sandbox_xxx
    ```

    ```typescript title="src/commet.ts" theme={null}
    import { Commet } from '@commet/node'

    export const commet = new Commet({
      apiKey: Bun.env.COMMET_API_KEY!,
    })
    ```
  </Step>

  <Step title="Suscribir">
    ```typescript title="src/index.ts" theme={null}
    import { commet } from './commet'

    Bun.serve({
      port: 3000,
      async fetch(req) {
        const url = new URL(req.url)

        if (url.pathname === '/subscribe' && req.method === 'POST') {
          const { customerId, email } = await req.json()

          await commet.customers.create({ email, id: customerId })

          const subscription = await commet.subscriptions.create({
            customerId,
            planCode: 'pro',
          })

          return Response.json({ checkoutUrl: subscription.checkoutUrl ?? null })
        }

        return new Response('Not Found', { status: 404 })
      },
    })
    ```
  </Step>

  <Step title="Verificar Acceso">
    Agrega estas rutas al handler `fetch`.

    ```typescript title="src/index.ts" theme={null}
    if (url.pathname.startsWith('/subscription/') && req.method === 'GET') {
      const customerId = url.pathname.split('/')[2]

      const subscription = await commet.subscriptions.getActive({ customerId })

      if (!subscription) {
        return Response.json({ error: 'no_active_subscription' }, { status: 404 })
      }

      return Response.json({ status: subscription.status })
    }

    if (url.pathname.startsWith('/features/') && req.method === 'GET') {
      const [, , feature, customerId] = url.pathname.split('/')

      const access = await commet.featureAccess.get({ code: feature, customerId })

      return Response.json({ allowed: access.allowed })
    }
    ```
  </Step>

  <Step title="Trackear Uso">
    ```typescript title="src/index.ts" theme={null}
    if (url.pathname === '/usage' && req.method === 'POST') {
      const { customerId } = await req.json()

      await commet.usage.track({
        customerId,
        featureCode: 'api_calls',
        value: 1,
      })

      return Response.json({ tracked: true })
    }
    ```

    El uso se agrega y se cobra al final del período.
  </Step>

  <Step title="Portal del Cliente">
    ```typescript title="src/index.ts" theme={null}
    if (url.pathname === '/portal' && req.method === 'GET') {
      const customerId = url.searchParams.get('customerId')!

      const portal = await commet.portal.getUrl({ customerId })

      return Response.redirect(portal.portalUrl)
    }
    ```
  </Step>
</Steps>

## Relacionado

* [Suscripciones](/docs/es/manage-subscriptions)
* [Trackear Uso](/docs/es/track-usage)
* [Portal del Cliente](/docs/es/customer-portal)
* [Referencia del SDK](/docs/es/sdk-reference)


## Related topics

- [Integrar con Astro](/docs/es/integrate-with-astro.md)
- [Integrar con Nuxt](/docs/es/integrate-with-nuxt.md)
- [Integrar con Hono](/docs/es/integrate-with-hono.md)
- [Integrar con Remix](/docs/es/integrate-with-remix.md)
- [Integrar con Next.js](/docs/es/integrate-with-nextjs.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.