Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

fix(outreach): support collection cursors and documented page offsets

Rudy Celekli2be5aa1986bc
diff --git a/tests/clis/outreach-pagination.test.cjs b/tests/clis/outreach-pagination.test.cjs
new file mode 100644
index 0000000..f291b25
--- /dev/null
+++ b/tests/clis/outreach-pagination.test.cjs
@@ -0,0 +1,48 @@
+const { test } = require('node:test')
+const assert = require('node:assert/strict')
+const { spawnSync } = require('node:child_process')
+const path = require('node:path')
+const cli = path.resolve(__dirname, '../../tools/clis/outreach.js')
+function run(args) {
+ const code = `global.fetch = async (url, options) => ({status:200,text:async()=>JSON.stringify({url,method:options.method,cursor:new URL(url).searchParams.get('page[after]')})});process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath, ['-e', code], { encoding: 'utf8', env: { ...process.env, OUTREACH_ACCESS_TOKEN: 'fixture-only' } })
+}
+for (const resource of ['prospects', 'sequences', 'mailings', 'accounts', 'tasks']) {
+ test(`${resource} list forwards cursor and size instead of repeating the first page`, () => {
+ const result = run([resource, 'list', '--after', 'opaque+cursor=/part', '--per-page', '25', '--sequence-id', '123', '--state', 'pending'])
+ assert.equal(result.status, 0, result.stderr)
+ const output = JSON.parse(result.stdout)
+ assert.equal(output.cursor, 'opaque+cursor=/part')
+ const query = new URL(output.url).searchParams
+ assert.equal(query.get('page[size]'), '25')
+ if (resource === 'mailings') assert.equal(query.get('filter[sequence][id]'), '123')
+ if (resource === 'tasks') assert.equal(query.get('filter[state]'), 'pending')
+ })
+}
+test('legacy page selection is translated into the documented offset contract', () => {
+ const result = run(['prospects', 'list', '--page', '3', '--per-page', '25'])
+ assert.equal(result.status, 0, result.stderr)
+ const query = new URL(JSON.parse(result.stdout).url).searchParams
+ assert.equal(query.get('page[offset]'), '50')
+ assert.equal(query.has('page[number]'), false)
+ assert.equal(query.has('page[size]'), false)
+ assert.equal(query.get('page[limit]'), '25')
+ const defaultQuery = new URL(JSON.parse(run(['prospects', 'list', '--page', '3']).stdout).url).searchParams
+ assert.equal(defaultQuery.get('page[offset]'), '100')
+ assert.equal(defaultQuery.get('page[limit]'), '50')
+})
+test('before cursor and dry-run preserve the exact token and credential redaction', () => {
+ const result = run(['accounts', 'list', '--before', 'previous+token', '--dry-run'])
+ assert.equal(result.status, 0, result.stderr)
+ const output = JSON.parse(result.stdout)
+ assert.equal(new URL(output.url).searchParams.get('page[before]'), 'previous+token')
+ assert.equal(output.headers.Authorization, 'Bearer ***')
+ assert.equal(new URL(JSON.parse(run(['accounts', 'list']).stdout).url).search, '')
+})
+test('contradictory or invalid page selections fail before any request', () => {
+ for (const args of [['--after','a','--before','b'],['--after','a','--page','2'],['--page','0'],['--page','1junk'],['--per-page'],['--per-page','1.5'],['--per-page','1001'],['--page','202','--per-page','50']]) {
+ const result = run(['accounts','list',...args])
+ assert.notEqual(result.status, 0)
+ assert.ok(JSON.parse(result.stderr).error)
+ }
+})
diff --git a/tools/clis/outreach.js b/tools/clis/outreach.js
index 63266bc..c133620 100755
--- a/tools/clis/outreach.js
+++ b/tools/clis/outreach.js
@@ -54,6 +54,32 @@ function parseArgs(args) {
const args = parseArgs(rawArgs)
const [cmd, sub, ...rest] = args._
+function paginationParams() {
+ const params = new URLSearchParams()
+ const after = args.after
+ const before = args.before
+ if (after !== undefined && before !== undefined) throw new Error('Use --after or --before, not both')
+ if ((after !== undefined || before !== undefined) && args.page !== undefined) throw new Error('Use cursor pagination or --page, not both')
+ for (const [name, value] of [['after', after], ['before', before]]) {
+ if (value !== undefined) {
+ if (typeof value !== 'string' || value.length === 0) throw new Error(`--${name} requires a cursor token`)
+ params.set(`page[${name}]`, value)
+ }
+ }
+ const size = args['per-page'] === undefined ? 50 : Number(args['per-page'])
+ if ((args['per-page'] !== undefined && (typeof args['per-page'] !== 'string' || !/^[0-9]+$/.test(args['per-page']))) || !Number.isSafeInteger(size) || size < 1 || size > 1000) throw new Error('--per-page must be an integer from 1 to 1000')
+ if (args['per-page'] !== undefined && args.page === undefined) params.set('page[size]', String(size))
+ if (args.page !== undefined) {
+ const page = Number(args.page)
+ if (typeof args.page !== 'string' || !/^[0-9]+$/.test(args.page) || !Number.isSafeInteger(page) || page < 1) throw new Error('--page must be a positive integer')
+ const offset = (page - 1) * size
+ if (!Number.isSafeInteger(offset) || offset > 10000) throw new Error('--page exceeds the provider offset limit; use --after instead')
+ params.set('page[offset]', String(offset))
+ params.set('page[limit]', String(size))
+ }
+ return params
+}
+
async function main() {
let result
@@ -61,9 +87,7 @@ async function main() {
case 'prospects':
switch (sub) {
case 'list': {
- const params = new URLSearchParams()
- if (args.page) params.set('page[number]', args.page)
- if (args['per-page']) params.set('page[size]', args['per-page'])
+ const params = paginationParams()
const qs = params.toString()
result = await api('GET', `/prospects${qs ? '?' + qs : ''}`)
break
@@ -92,7 +116,9 @@ async function main() {
case 'sequences':
switch (sub) {
case 'list': {
- result = await api('GET', '/sequences')
+ const params = paginationParams()
+ const qs = params.toString()
+ result = await api('GET', `/sequences${qs ? '?' + qs : ''}`)
break
}
case 'get': {
@@ -136,7 +162,7 @@ async function main() {
case 'mailings':
switch (sub) {
case 'list': {
- const params = new URLSearchParams()
+ const params = paginationParams()
if (args['sequence-id']) params.set('filter[sequence][id]', args['sequence-id'])
const qs = params.toString()
result = await api('GET', `/mailings${qs ? '?' + qs : ''}`)
@@ -150,7 +176,9 @@ async function main() {
case 'accounts':
switch (sub) {
case 'list': {
- result = await api('GET', '/accounts')
+ const params = paginationParams()
+ const qs = params.toString()
+ result = await api('GET', `/accounts${qs ? '?' + qs : ''}`)
break
}
case 'get': {
@@ -167,7 +195,7 @@ async function main() {
case 'tasks':
switch (sub) {
case 'list': {
- const params = new URLSearchParams()
+ const params = paginationParams()
const state = args.state || args.status
if (state) params.set('filter[state]', state)
const qs = params.toString()
@@ -183,6 +211,7 @@ async function main() {
result = {
error: 'Unknown command',
usage: {
+ pagination: 'All list commands: [--after <cursor> | --before <cursor> | --page <n>] [--per-page <n>]',
prospects: {
list: 'prospects list [--page <n>] [--per-page <n>]',
get: 'prospects get --id <id>',
diff --git a/tools/integrations/outreach.md b/tools/integrations/outreach.md
index 763ebcb..eee44c7 100644
--- a/tools/integrations/outreach.md
+++ b/tools/integrations/outreach.md
@@ -135,9 +135,20 @@ curl -s "https://api.outreach.io/api/v2/tasks?filter[state]=incomplete" \
## Parameters
+### Collection pagination
+
+All CLI list commands support `--after <cursor>` or `--before <cursor>` plus `--per-page <n>` (maximum 1000). Take the opaque token from the API's `links.next` or `links.prev` URL; the CLI encodes it as `page[after]` or `page[before]` and preserves existing resource filters.
+
+```bash
+node tools/clis/outreach.js tasks list --state pending --after '<next-cursor>' --per-page 50
+node tools/clis/outreach.js mailings list --sequence-id 123 --after '<next-cursor>'
+```
+
+The existing `--page <n>` spelling remains a compatibility option: it translates to the documented `page[offset]` and `page[limit]` using the requested size (default 50). Outreach deprecates offset pagination and caps the offset at 10,000; prefer cursor pagination for large collections. Cursor directions and `--page` cannot be combined. Default unfiltered requests remain unchanged. The CLI performs one request per invocation.
+
+Source: [Outreach request and pagination contracts](https://developers.outreach.io/api/making-requests#pagination).
+
### Prospects
-- `page[number]` - Page number (default: 1)
-- `page[size]` - Results per page (default: 25, max: 1000)
- `filter[emails]` - Filter by email
- `filter[firstName]` - Filter by first name
- `filter[lastName]` - Filter by last name
@@ -181,3 +192,7 @@ curl -s "https://api.outreach.io/api/v2/tasks?filter[state]=incomplete" \
The task filter is `state` (`pending`, `incomplete`, or `complete`). Use
`tasks list --state incomplete`; the CLI retains `--status` as an alias.
See [discover open tasks](https://developers.outreach.io/api/common-patterns).
+
+Without pagination flags the CLI leaves provider defaults untouched. The legacy
+`--page` option explicitly uses a default limit of 50 when `--per-page` is absent;
+cursor pagination uses `page[size]`, while legacy offsets use `page[limit]`.