> For the complete documentation index, see [llms.txt](https://swiftcord.gitbook.io/discordkit-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://swiftcord.gitbook.io/discordkit-guide/build-a-bot/logging-in.md).

# Logging In

The most crucial step - logging in to Discord's bot API

The first step to talk to Discord is always logging in.

In your `Bot.swift` (or equivalent) file, you'll need to add the following line to your `main()` function:

<pre class="language-swift" data-line-numbers><code class="lang-swift">// Login to Discord's API
bot.login()

// Run the main RunLoop to prevent the program from exiting
// If this line throws a compile error for you, make sure you imported Foundation!
<a data-footnote-ref href="#user-content-fn-1">RunLoop.main.run()</a>
</code></pre>

As simple as that: if you run your app now (assuming you've added your token correctly), it'll attempt to connect to the Discord Gateway and set the REST API handler, ready for API requests. You should see something like the following at the bottom of your console:

<figure><img src="https://1903821505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWJuHiYLW9jKqPb7h8D7t%2Fuploads%2FS3HOdsNuTFV2UAnmSZzp%2FScreenshot%202022-12-15%20at%2011.39.50%20AM.png?alt=media&amp;token=67e20927-ec44-4352-a12d-6b4f3e7b34eb" alt=""><figcaption></figcaption></figure>

However, most of the time you'd like to listen to some events, which can be done by adding the code below to your `main()` function, before the call to `bot.login()`:

{% code lineNumbers="true" %}

```swift
bot.ready.listen {
    print("Successfully logged in as \(bot.user!.username)#\(bot.user!.discriminator)!")
}
```

{% endcode %}

Rerun your app, and you should see that message printed out in the console shortly after!

<figure><img src="https://1903821505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWJuHiYLW9jKqPb7h8D7t%2Fuploads%2F2TO2eBWq9AwN64yPaetp%2FScreenshot%202022-12-15%20at%2011.40.43%20AM.png?alt=media&amp;token=910dd15e-09e3-4344-b26a-e3cfdc2717d9" alt=""><figcaption><p>The sweet taste of success</p></figcaption></figure>

{% embed url="<https://github.com/SwiftcordApp/DiscordKitGettingStarted/tree/logging-in>" %}
Use this branch as a reference for how your project should look!
{% endembed %}

[^1]: Keep in mind that this function never returns, so none of the lines following it will be executed!
