working version with javascript api