# messages.search

Gets back found messages

## Example

{% tabs %}
{% tab title="Javascript" %}

```javascript
const {Api, TelegramClient} = require('telegram');
const {StringSession} = require('telegram/sessions');

const session = new StringSession('');
const client = new TelegramClient(session, apiId, apiHash, {});

(async function run() {
    const result = await client.invoke(new Api.messages.search({
        peer: new Api.InputPeer({...}),
        q: 'random string here',
        fromId: new Api.InputPeer({...}),
        topMsgId: 4881049,
        filter: new Api.MessagesFilter({...}),
        minDate: 7108524,
        maxDate: 3588179,
        offsetId: 840380,
        addOffset: 3671533,
        limit: 6577178,
        maxId: 7375544,
        minId: 9351693,
        hash: 7383860,
        }));
    console.log(result); // prints the result
})();
```

{% endtab %}

{% tab title="TypeScript" %}

```typescript
import {Api, TelegramClient} from 'telegram';
import {StringSession} from 'telegram/sessions';

const session = new StringSession('');
const client = new TelegramClient(session, apiId, apiHash, {});

(async function run() {
    const result: Api.messages.Messages = await client.invoke(new Api.messages.search({
        peer: new Api.InputPeer({...}),
        q: 'random string here',
        fromId: new Api.InputPeer({...}),
        topMsgId: 4881049,
        filter: new Api.MessagesFilter({...}),
        minDate: 7108524,
        maxDate: 3588179,
        offsetId: 840380,
        addOffset: 3671533,
        limit: 6577178,
        maxId: 7375544,
        minId: 9351693,
        hash: 7383860,
        }));
    console.log(result); // prints the result
})();
```

{% endtab %}
{% endtabs %}

## TL schema

```
messages.messages#8c718e87 messages:Vector<Message> chats:Vector<Chat> users:Vector<User> = messages.Messages;
messages.messagesSlice#3a54685e flags:# inexact:flags.1?true count:int next_rate:flags.0?int offset_id_offset:flags.2?int messages:Vector<Message> chats:Vector<Chat> users:Vector<User> = messages.Messages;
messages.channelMessages#64479808 flags:# inexact:flags.1?true pts:int count:int offset_id_offset:flags.2?int messages:Vector<Message> chats:Vector<Chat> users:Vector<User> = messages.Messages;
messages.messagesNotModified#74535f21 count:int = messages.Messages;
---functions---
messages.search#c352eec flags:# peer:InputPeer q:string from_id:flags.0?InputPeer top_msg_id:flags.1?int filter:MessagesFilter min_date:int max_date:int offset_id:int add_offset:int limit:int max_id:int min_id:int hash:int = messages.Messages;
```

## Parameters

|       Name       | Type                                                                                                                                 | Description                                                                                                                                                |
| :--------------: | ------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
|     **flags**    | [#](https://core.telegram.org/type/%23)                                                                                              | Flags, see [TL conditional fields](https://core.telegram.org/mtproto/TL-combinators#conditional-fields)                                                    |
|     **peer**     | [InputPeer](https://core.telegram.org/type/InputPeer)                                                                                | User or chat, histories with which are searched, or [(inputPeerEmpty)](https://core.telegram.org/constructor/inputPeerEmpty) constructor for global search |
|       **q**      | [string](https://core.telegram.org/type/string)                                                                                      | Text search request                                                                                                                                        |
|   **from\_id**   | [flags](https://core.telegram.org/mtproto/TL-combinators#conditional-fields).0?[InputPeer](https://core.telegram.org/type/InputPeer) | Only return messages sent by the specified user ID                                                                                                         |
| **top\_msg\_id** | [flags](https://core.telegram.org/mtproto/TL-combinators#conditional-fields).1?[int](https://core.telegram.org/type/int)             | [Thread ID](https://core.telegram.org/api/threads)                                                                                                         |
|    **filter**    | [MessagesFilter](https://core.telegram.org/type/MessagesFilter)                                                                      | Filter to return only specified message types                                                                                                              |
|   **min\_date**  | [int](https://core.telegram.org/type/int)                                                                                            | If a positive value was transferred, only messages with a sending date bigger than the transferred one will be returned                                    |
|   **max\_date**  | [int](https://core.telegram.org/type/int)                                                                                            | If a positive value was transferred, only messages with a sending date smaller than the transferred one will be returned                                   |
|  **offset\_id**  | [int](https://core.telegram.org/type/int)                                                                                            | Only return messages starting from the specified message ID                                                                                                |
|  **add\_offset** | [int](https://core.telegram.org/type/int)                                                                                            | [Additional offset](https://core.telegram.org/api/offsets)                                                                                                 |
|     **limit**    | [int](https://core.telegram.org/type/int)                                                                                            | [Number of results to return](https://core.telegram.org/api/offsets)                                                                                       |
|    **max\_id**   | [int](https://core.telegram.org/type/int)                                                                                            | [Maximum message ID to return](https://core.telegram.org/api/offsets)                                                                                      |
|    **min\_id**   | [int](https://core.telegram.org/type/int)                                                                                            | [Minimum message ID to return](https://core.telegram.org/api/offsets)                                                                                      |
|     **hash**     | [int](https://core.telegram.org/type/int)                                                                                            | [Hash](https://core.telegram.org/api/offsets)                                                                                                              |

## Result

[messages.Messages](https://core.telegram.org/type/messages.Messages)

## Possible errors

| Code | Type                        | Description                                  |
| :--: | --------------------------- | -------------------------------------------- |
|  400 | CHANNEL\_INVALID            | The provided channel is invalid              |
|  400 | CHANNEL\_PRIVATE            | You haven't joined this channel/supergroup   |
|  400 | CHAT\_ADMIN\_REQUIRED       | You must be an admin in this chat to do this |
|  400 | INPUT\_CONSTRUCTOR\_INVALID | The provided constructor is invalid          |
|  400 | INPUT\_USER\_DEACTIVATED    | The specified user was deleted               |
|  400 | MSG\_ID\_INVALID            | Invalid message ID provided                  |
|  400 | PEER\_ID\_INVALID           | The provided peer id is invalid              |
|  400 | PEER\_ID\_NOT\_SUPPORTED    | The provided peer ID is not supported        |
|  400 | SEARCH\_QUERY\_EMPTY        | The search query is empty                    |
|  400 | USER\_ID\_INVALID           | The provided user ID is invalid              |

## Can bots use this methd ?

### yes

## Related pages

### [inputPeerEmpty](https://core.telegram.org/constructor/inputPeerEmpty)

An empty constructor, no user or chat is defined.

### [Threads](https://core.telegram.org/api/threads)

Telegram allows commenting on a [channel post](https://core.telegram.org/api/channel) or on a generic [supergroup message](https://core.telegram.org/api/channel), thanks to message threads.

### [Pagination in the API](https://core.telegram.org/api/offsets)

How to fetch results from large lists of objects.
