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 Name

Type

Description

preferenceName

string

Name of the key-value storage file. You can have multiple key-value storage files for your app. Should be a non-empty string.

key

string

Name of the key to fetch the value for.

defaultValue

undefined/string

Default 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:

Code

Description

KEY_NOT_FOUND

No 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 Name

Type

Description

preferenceName

string

Name of the key-value storage file. You can have multiple key-value storage files for your app.

key

string

Name of the key to fetch the value for.

value

string

Value 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')