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

# autClaw JavaScript plugin reference

> Use autClaw middleware.js for JavaScript plugins, including the entry point, Sender, message replies, storage, routes, and cron tasks.

`plugin/scripts/middleware.js` is the SDK for regular JavaScript plugins. Use it to create `Sender`, then read the current runtime context, reply to messages, save data, wait for input, handle payments, download user-uploaded files, handle HTTP routes, push proactively, and manage runtime scheduled tasks.

<Note>
  New plugins should prefer `const { Sender, getSenderID } = require('./middleware.js')`. Do not hard-code runtime credentials, ports, or autClaw internal service addresses.
</Note>

## Quick start

```js theme={null}
//[author: your-name]
//[runtime: node@22.22.1]
//[title: Echo]
//[rule: echo (.+)]
const { Sender, getSenderID } = require('./middleware.js')

async function main() {
  const sender = new Sender(getSenderID())
  const text = await sender.param(1)
  await sender.reply(text || 'Please enter text')
}

main().catch((error) => {
  console.error(error && error.stack ? error.stack : error)
})
```

## Browse by task

<CardGroup cols={2}>
  <Card title="Context and base types" icon="circle-dot" href="/en/plugin-sdk/javascript/context">
    Import style, `senderID`, response wrappers, `SendReceipt`, `MediaItem`, and download return bodies.
  </Card>

  <Card title="Sender message capabilities" icon="message-circle" href="/en/plugin-sdk/javascript/sender-messages">
    Read users and chats, match parameters, reply with text/media, recall, and use `replyMixed(...)`.
  </Card>

  <Card title="Wait for input and payment" icon="hourglass" href="/en/plugin-sdk/javascript/wait-and-payment">
    `listen(...)`, `input(...)`, `waitPay(exitcode, timeout, amount?)`, and amount-matching details.
  </Card>

  <Card title="Events, media, and files" icon="file-down" href="/en/plugin-sdk/javascript/media-files">
    `getMediaItems()`, `fileDownload(...)`, `downloadAdapterFile(...)`, and `getGroupMemberList(...)`.
  </Card>

  <Card title="Storage and routes" icon="database" href="/en/plugin-sdk/javascript/storage-and-routes">
    Default storage, named buckets, and HTTP router requests and responses.
  </Card>

  <Card title="Proactive push" icon="send" href="/en/plugin-sdk/javascript/push">
    Targets, account selection, and return values for `push(...)` / `pushImage(...)` / `pushMixed(...)`.
  </Card>

  <Card title="Tools, Cron, and group management" icon="wrench" href="/en/plugin-sdk/javascript/tools-cron-group">
    `importModule(...)`, `importJs(...)`, system utilities, `Cron`, group invite/mute/announcement.
  </Card>

  <Card title="TypeScript declarations" icon="braces">
    Machine-readable signatures are available in `en/plugin-sdk/types/middleware-js.d.ts` for IDEs and AI retrieval.
  </Card>
</CardGroup>

## Common imports

```js theme={null}
const {
  Sender,
  Cron,
  getSenderID,
  bucketGet,
  bucketSet,
  fileDownload,
  downloadAdapterFile,
  getGroupMemberList,
  importModule,
  push,
  pushImage,
  pushMixed,
} = require('./middleware.js')

const sender = new Sender(getSenderID())
```

<Info>
  Most methods in `middleware.js` are asynchronous and throw `Error` on failure. Use `try/catch` in flows that send network requests, media, or files, and reply with an actionable error message.
</Info>

## Recommended template

```js theme={null}
//[author: your-name]
//[runtime: node@22.22.1]
//[title: Safe template]
//[rule: demo (.+)]
const { Sender, getSenderID } = require('./middleware.js')

async function main() {
  const sender = new Sender(getSenderID())
  const value = await sender.param(1)

  if (!value) {
    await sender.reply('Please enter text, for example: demo hello')
    return
  }

  await sender.reply(`Received: ${value}`)
}

main().catch((error) => {
  console.error(error && error.stack ? error.stack : error)
})
```

## Avoid these patterns

* Do not hard-code runtime credentials, ports, or service addresses.
* Do not bypass the SDK to call autClaw services directly.
* Do not manually parse platform raw file fields. Use SDK download helpers.
* Do not put large datasets into one bucket key. Split data across keys for easier maintenance.
