Store data from code steps with StoreClient

StoreClient is a built-in utility available in both Python and JavaScript code steps that lets you store and retrieve data between workflows or between runs of the same workflow. Use it when you need to track counters, deduplicate data, or share state across runs.

Prerequisites

  • A Code by Zapier step in your workflow.
  • A UUID4 secret to protect your stored data. You can generate one with the Online UUID Generator Tool.

1. Create a StoreClient

In your Code step, create a StoreClient by passing your UUID4 secret:

JavaScript Python
var store = StoreClient('your secret here');
Note

StoreClient is promise-based. Use async and await to work with it.

2. Get and set values

JavaScript Python
const store = StoreClient('your secret here');
await store.set('hello', 'world');
const value = await store.get('hello');
return {result: value} // value === 'world'

If the value does not exist, get() returns null.

3. Bulk operations

JavaScript Python

You can save and retrieve multiple keys and values at once:

const store = StoreClient('your secret here');
await store.setMany({hello: 'world', foo: 'bar'})
const values = await store.getMany('hello', 'foo');
// values === {hello: 'world', foo: 'bar'}
await store.deleteMany('hello', 'foo');
// or, if you want to wipe everything
await store.clear();

You can call getMany and deleteMany in a few different ways:

store.getMany('hello', 'foo'); // as arguments
store.getMany(['hello', 'foo']); // as array
store.getMany({hello: null, foo: null}); // as object

4. Additional Python operations

Python offers additional operations for incrementing values, conditionally setting values, and manipulating lists. These operations are safe to use when multiple workflows access the same key at the same time.

Increment a value

increment_by increments a numeric value stored under a given key:


    store.increment_by(key, amount)

Conditionally set a value

set_value_if sets a value only if the current value matches a given previous value:


    store.set(key, 1) # set a value
# sets 2 only if the previous value of *key* is 1
store.set_value_if(key, value=2, previous_value=1)

Store and remove nested values

set_child_values and remove_child_values work with dictionary values stored under a key:


    store.set_child_values(key, {'a': 'b'}) # store under the given key the key `a` with value `b`
store.remove_child_values(key, ['a', 'c']) # remove the keys `a` and 'c' from the mapping found at `key`

Manipulate lists

list_push and list_pop add and remove items from a list:


    store.list_push(key, some_value)
store.list_pop(key)

Both accept a location parameter:


    store.list_push(key, value, location='tail') # Push to the tail of the list
store.list_push(key, value, location='tail_set') # Push to the tail only if the value is not already in the list
store.list_push(key, value, location='head') # Push to the head of the list
store.list_push(key, value, location='head_set') # Push to the head only if the value is not already in the list
store.list_pop(key, location='tail') # Pop from the end of the list
store.list_pop(key, location='head') # Pop from the head of the list

list_pop also accepts a default parameter, which is returned when the list is empty:


    store.list_pop(key, default=1, location='tail')

Limitations

  • The secret must use UUID4 format.
  • Every key must be fewer than 32 characters.
  • Every value must be fewer than 2,500 bytes.
  • You can store up to 500 keys per secret.
  • Keys expire after three months of inactivity.
  • Only JSON-serializable values can be stored.

Related resources

Was this article helpful?
4 out of 5 found this helpful