added encode() function for apis