← Back to Recipes

Polling an item connector's execution status

How to check and poll for the Item status, to know when the Item execution finishes and be able to act on it.

Steps#

1. Setup SDK#

Setup the PluggyClient with your application credentials.

2. Start polling status#

Since UPDATING status means that the connection is syncing with the provider, you should start fetching in intervals to get the new item status.

3. Return item#

Return the item with the new status. See more information about item statuses here.

Code#

import { Item, PluggyClient } from 'pluggy-sdk'
 
function sleep(ms: number): Promise<void> {
  return new Promise(resolve => {
    setTimeout(resolve, ms)
  })
}
 
const { CLIENT_ID = '', CLIENT_SECRET = '' } = process.env
 
const client = new PluggyClient({
  clientId: CLIENT_ID,
  clientSecret: CLIENT_SECRET,
})
 
async function waitForItemExecutionFinish(itemId: string): Promise<Item> {
  // poll item status, until it's connection finishes
  let item = await client.fetchItem(itemId)
  console.log(`Item status: ${item.status}, execution status: ${item.executionStatus}`)
 
  while (item.status === 'UPDATING') {
    // wait a few seconds before next request (to prevent 429 error response)
    await sleep(2000)
 
    // retrieve item status
    item = await client.fetchItem(item.id)
    console.log(`Item status: ${item.status}, execution status: ${item.executionStatus}`)
  }
 
	// item's connection has finished, check the result to act on it
  // (item.status may be: 'LOGIN_ERROR', 'OUTDATED', 'WAITING_USER_INPUT', or 'UPDATED')
  // see docs for more info: https://docs.pluggy.ai/docs/item-lifecycle
  return item
}
 
const item = await waitForItemExecutionFinish('my-item-id-1234')
 
console.log('Item status polling finished, result:', item)

Example Response#

{
  "id": "2242624c...",
  "connector": {},
  "createdAt": "2022-10-20T13:38:04.056Z",
  "updatedAt": "2022-10-21T04:18:08.000Z",
  "status": "UPDATED",
  "executionStatus": "SUCCESS",
  "lastUpdatedAt": "2022-10-21T04:18:08.000Z",
  "webhookUrl": null,
  "error": null,
  "clientUserId": null,
  "statusDetail": null,
  "parameter": null
}