Key-Value Storage


PhonePe provides a native persistent key-value storage. Both the keys and values can only be of type string.

getItem(preferenceName, key, defaultValue)

Description: This method is used to get a stored value from native key-value storage

Request Parameters

Parameter NameTypeDescription
preferenceNamestringName of the key-value storage file. You can have multiple key-value storage files for your app. Should be a non-empty string.
keystringName of the key to fetch the value for.
defaultValueundefined/stringDefault value in case the key doesn't exist.

Usage:

sdk.getItem('app', 'key', null)
  .then((data) => {
    console.log('Value received = ' + data)
})
  .catch((err) => {})

Response Parameter

Promise<string>
A promise with the desired value or error if there is some issue. Promise failure reasons can be as follows:

CodeDescription
KEY_NOT_FOUNDNo value found for this key and no default value was provided
Promise resolve:
'valueForKey'

Promise reject:
{
  'error_code': 'KEY_NOT_FOUND'
}

setItem(preferenceName, key, defaultValue)

Description: This method is used to store a value (string) for the corresponding key (string) into the key-value storage.

Request Parameters

Parameter NameTypeDescription
preferenceNamestringName of the key-value storage file. You can have multiple key-value storage files for your app.
keystringName of the key to fetch the value for.
valuestringValue that you would like to set for the key

Response Parameter

void

Usage:

sdk.setItem('app', 'key', 'value')

removeItem(preferenceName, key)

Description: This method is used remove a key-value pair from key-value storage.
Usage:

sdk.removeItem('app', 'key')