# String Session

You'll need a API\_ID and API\_HASH in order to generate pyrogram session. Always remember to use good API combo else your account could be deleted.

{% hint style="info" %}
How to get API\_ID and API\_HASH ?

* Go to [my.telegram.org](https://my.telegram.org/) then Enter your Phone Number with your country code.
* After, you are logged in click on [API Development Tools](https://my.telegram.org/apps).
* Enter Anything as App name and App short name, Enter my.telegram.org in url section
* That’s it, You”ll get your **API\_ID ans API\_HASH**.
  {% endhint %}

## Generate Session through local/VPS&#x20;

### <mark style="color:red;">Method 1</mark>

Simply run this code and give the input which is being asked and you will get String Session

```bash
#!/bin/bash

# Check if pyrogram is installed and the version is >= 2.0.106
python -c "try:\n    import pyrogram\n    if hasattr(pyrogram, '__version__') and tuple(map(int, pyrogram.__version__.split('.'))) >= (2, 0, 106):\n        print('Pyrogram version is sufficient.')\n        exit(0)\n    else:\n        print('Pyrogram version is older than 2.0.106.')\n        exit(1)\nexcept ImportError:\n    print('Pyrogram is not installed.')\n    exit(1)"

# Install pyrogram if not installed or version is older
echo "Installing pyrogram (version >= 2.0.106)..."
pip install pyrogram>=2.0.106

echo "\n=== Telegram API Credentials ==="
read -p "Enter Your API_ID: " api_id
read -p "Enter Your API_HASH: " api_hash

echo "\nEnter Phone number when asked.\n"

session_name=":memory:"

python -c "
import asyncio
import os
from pyrogram import Client as c

async def main():
    api_id = '$api_id'
    api_hash = '$api_hash'
    session_name = '$session_name'
    i = c(session_name, api_id=api_id, api_hash=api_hash)

    try:
        await i.start()
        ss = await i.export_session_string()
        xx = f'HERE IS YOUR STRING SESSION, COPY IT, DON\\'T SHARE!!\\n\\n`{ss}`\\n\\nGENERATED BY Stranger'
        await i.send_message('me', xx)
        print('\\nHERE IS YOUR STRING SESSION, COPY IT, DON\\'T SHARE!!\\n')
        print(f'\\n{ss}\\n')
        print('\\nGENERATED BY Stranger\\n')
    finally:
        session_file = f'{session_name}.session'
        if os.path.exists(session_file):
            os.remove(session_file)

if __name__ == '__main__':
    asyncio.run(main())
"
```

#### **How to Run:**

1. Save the code above in a file named `start.sh` (or any other name with a `.sh` extension).
2. Make the script executable: `chmod +x start.sh`
3. Run the script: `./start.sh`&#x20;

### <mark style="color:red;">Method 2</mark>

#### Clone the Stranger repo and run the gensession file&#x20;

```bash
git clone https://github.com/xbitcode/StrangerMusicBot \
&& cd StrangerMusicBot
```

```bash
bash gensession
```

#### Enter the details for getting the string session&#x20;

* API\_ID
* API\_HASH
* and enter the phone number when asked

#### Clean the files

```bash
cd .. \
&& sudo rm -r StrangerMusicBot
```
