> ## 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 Express

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

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">
    <Tabs>
      <Tab title="pnpm">
        ```bash theme={null}
        pnpm add @commet/node express
        ```
      </Tab>

      <Tab title="npm">
        ```bash theme={null}
        npm install @commet/node express
        ```
      </Tab>

      <Tab title="yarn">
        ```bash theme={null}
        yarn add @commet/node express
        ```
      </Tab>
    </Tabs>
  </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: process.env.COMMET_API_KEY!,
    })
    ```
  </Step>

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

    const router = Router()

    router.post('/subscribe', async (req, res) => {
      const { customerId, email } = req.body

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

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

      res.json({ checkoutUrl: subscription.checkoutUrl ?? null })
    })

    export default router
    ```
  </Step>

  <Step title="Verificar Acceso">
    ```typescript title="src/routes/billing.ts" theme={null}
    router.get('/subscription/:customerId', async (req, res) => {
      const subscription = await commet.subscriptions.getActive({ customerId: req.params.customerId })

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

      res.json({ status: subscription.status })
    })

    router.get('/features/:feature/:customerId', async (req, res) => {
      const access = await commet.featureAccess.get({
        code: req.params.feature,
        customerId: req.params.customerId,
      })

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

  <Step title="Trackear Uso">
    ```typescript title="src/routes/billing.ts" theme={null}
    router.post('/usage', async (req, res) => {
      await commet.usage.track({
        customerId: req.body.customerId,
        featureCode: 'api_calls',
        value: 1,
      })

      res.json({ tracked: true })
    })
    ```

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

  <Step title="Portal del Cliente">
    ```typescript title="src/routes/billing.ts" theme={null}
    router.get('/portal', async (req, res) => {
      const portal = await commet.portal.getUrl({
        customerId: req.user.customerId,
      })

      res.redirect(portal.portalUrl)
    })
    ```
  </Step>

  <Step title="Iniciar el Servidor">
    ```typescript title="src/index.ts" theme={null}
    import express from 'express'
    import billingRoutes from './routes/billing'

    const app = express()

    app.use(express.json())
    app.use('/billing', billingRoutes)

    app.listen(3000)
    ```
  </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 Flask](/docs/es/integrate-with-flask.md)
- [Integrar con Java](/docs/es/integrate-with-java.md)
- [Integrar con Django](/docs/es/integrate-with-django.md)
- [Integrar con Encore](/docs/es/integrate-with-encore.md)
- [Integrar con Laravel](/docs/es/integrate-with-laravel.md)


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