TypeScript isomorphic library to make work with Telegram Web Apps init data easier.

Overview

Telegram Web Apps Init Data

NPM

TypeScript isomorphic library to make work with Telegram Web Apps init data easier. Feel free to use it in browser and Node JS.

Installation

npm i twa-init-data

or

yarn add twa-init-data

Usage

Parsing

This library contains function called parse which is able to convert init data presented as string or search params to more comfortable object with known parameters InitData.

Here comes the example of usage:

import {parse} from 'twa-init-data';

const initDataRaw = 'query_id=AAHdF6IQAAAAAN0XohDhrOrc&user=%7B%22id%22%3A279058397%2C%22first_name%22%3A%22Vladislav%22%2C%22last_name%22%3A%22Kibenko%22%2C%22username%22%3A%22vdkfrost%22%2C%22language_code%22%3A%22ru%22%2C%22is_premium%22%3Atrue%7D&auth_date=1662771648&hash=c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2';

console.log(parse(initDataRaw));
// {
//   authDate: 2022-09-10T01:00:48.000Z,
//   hash: 'c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2',
//   queryId: 'AAHdF6IQAAAAAN0XohDhrOrc',
//   user: {
//   id: 279058397,
//     firstName: 'Vladislav',
//     lastName: 'Kibenko',
//     username: 'vdkfrost',
//     languageCode: 'ru',
//     isPremium: true
//   }
// }

Function extracts required parameters and automatically validates their types. It will throw an error in case, some property has invalid type or value.

Validation

To validate init data sign, function validate is used. It expects passing init data presented in raw format (search params) and throws an error in case, something is wrong. To see full list of errors, please, check function's JSDoc. It does not return any result.

import {validate} from 'twa-init-data';

const initDataRaw = 'query_id=AAHdF6IQAAAAAN0XohDhrOrc&user=%7B%22id%22%3A279058397%2C%22first_name%22%3A%22Vladislav%22%2C%22last_name%22%3A%22Kibenko%22%2C%22username%22%3A%22vdkfrost%22%2C%22language_code%22%3A%22ru%22%2C%22is_premium%22%3Atrue%7D&auth_date=1662771648&hash=c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2';
const secretToken = '5768337691:AAH5YkoiEuPk8-FZa32hStHTqXiLPtAEhx8';

validate(initDataRaw, secretToken);

By default, function checks init data expiration. Default expiration duration is 1 day (86_400 seconds). It is recommended to always check init data expiration as long as it could be stolen, but still valid. To disable this feature, use third function argument:

import {validate} from 'twa-init-data';

const initDataRaw = 'query_id=AAHdF6IQAAAAAN0XohDhrOrc&user=%7B%22id%22%3A279058397%2C%22first_name%22%3A%22Vladislav%22%2C%22last_name%22%3A%22Kibenko%22%2C%22username%22%3A%22vdkfrost%22%2C%22language_code%22%3A%22ru%22%2C%22is_premium%22%3Atrue%7D&auth_date=1662771648&hash=c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2';
const secretToken = '5768337691:AAH5YkoiEuPk8-FZa32hStHTqXiLPtAEhx8';

validate(initDataRaw, secretToken, {expiresIn: 0});
You might also like...

An easy-to-use library to make your life easier when working with random numbers or random choices in javascript.

vrandom An easy-to-use library to make your life easier when working with random numbers or random choices in javascript. Table of contents Installati

Aug 16, 2022

The Easel Javascript library provides a full, hierarchical display list, a core interaction model, and helper classes to make working with the HTML5 Canvas element much easier.

EaselJS EaselJS is a library for building high-performance interactive 2D content in HTML5. It provides a feature-rich display list to allow you to ma

Dec 29, 2022

Make drag-and-drop easier using DropPoint. Drag content without having to open side-by-side windows

Make drag-and-drop easier using DropPoint. Drag content without having to open side-by-side windows

Make drag-and-drop easier using DropPoint! DropPoint helps you drag content without having to open side-by-side windows Works on Windows, Linux and Ma

Dec 29, 2022

🤝 A set of Persian Helpers for NodeJS to make your life easier

Persian Helpers Persian Helpers is a minimal NodeJS package with a set of helpers/tools specifically for the Persian/Farsi language. If you like the p

Dec 22, 2022

A script that implements a GUI to make cheating on Blooket easier than ever.

BlooketUI What's BlooketUI? A script that implements a GUI to make cheating on Blooket easier than ever. How do i Use This? Copy the code of src.js by

Dec 24, 2022

Helps make it easier to utilize flyte from vs-code

Flyte-Wingman This is the repository for the Flyte-Wingman vscode extension. This extension can be used to easily create, serialize, and register pyth

Jun 13, 2022

A custom element that aims to make it easier to embed Spring '83 boards

spring-board element A custom element that makes it simple to embed Spring '83 boards! Usage If you are using spring-board in a client-side framew

Jan 1, 2023

A tampermonkey script that adds functionality to the midjourney.com website to make it easier to do things.

A tampermonkey script that adds functionality to the midjourney.com website to make it easier to do things.

MidJourneyTools A tampermonkey script that adds functionality to the midjourney.com website to make it easier to do things. Setup Instructions Make su

Dec 24, 2022

Functions and objects that make it easier to add fields to Portable Text editors for accessibility meta information, like language changes or abbreviations.

Porta11y Porta11y is a collection of accessibility-focused annotations, decorators and validators for Sanity’s Portable Text editor. Portable Text is

Aug 25, 2022
Owner
Telegram Web Apps
Telegram Web Apps enthusiasts community.
Telegram Web Apps
A JavaScript Library To Make Your Work Work Easier/Faster

Functionality.js (beta) About ✍️ This Is A JavaScript Library To Make Your Work Easier/Faster, You Can See Functionalty.js Website From Here Project C

Ali-Eldeba 9 May 25, 2022
A JavaScript Library To Make Your Work Work Easier/Faster

Functionality.js About ✍️ This Is A JavaScript Library To Make Your Work Easier/Faster, You Can See Functionalty.js Website From Here Project Created

functionality 16 Jun 23, 2022
With this plugin, you can easily make a stopwatch or timer on your site. Just init, style and enjoy.

TimezZ With this plugin, you can easily make a stopwatch or timer on your site. Just init, style and enjoy. Features Typescript support Support all en

Valery Strelets 37 Dec 5, 2022
why make apps to increase focus -- when you can make apps to reduce focus

impossifocus ?? What is this? ImpossiFocus will measure focus by reading your brainwaves -- and if you're in the zone, it'll ensure that changes with

Aleem Rehmtulla 10 Nov 30, 2022
Init a target by promise only once.

once-init ?? Let Promise Function Executed Only Once. The Promise will be executed when the attribute target is called for the first time, and the Pro

Xmo 65 Dec 26, 2022
This is college project in which me and my team create a website that provide the tools for basic text modification and add todos also we add blog init.

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Ayush 4 Jun 9, 2022
A CLI tool to create a NodeJS project with TypeScript CTSP is a CLI tool to make easier to start a new NodeJS project and configure Typescript on it.

CTSP- Create TS Project A CLI tool to create a NodeJS project with TypeScript CTSP is a CLI tool to make easier to start a new NodeJS project and conf

Jean Rodríguez 7 Sep 13, 2022
A tiny isomorphic fast function for generating a cryptographically random hex string.

ZeptoID A tiny isomorphic fast function for generating a cryptographically random hex string. Accoding to this calculator one would have to generate i

Fabio Spampinato 9 Oct 24, 2022
All in one is a CLI to make your journey in web development less painful (it makes your life way easier).

All In One CLI (Aio) The Ultimate CLI for all your needs in web development. Description This is a CLI that has all the commands you need to do anythi

Я♡M...∞ 17 Sep 25, 2022