# Generating a login email via API

**URL:** https://meta.discourse.org/t/generating-a-login-email-via-api/235737
**Category:** Development
**Tags:** rest-api
**Created:** [August 10, 2022, 2:55pm UTC](https://meta.discourse.org/t/generating-a-login-email-via-api/235737 "2022-08-10T14:55:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![davidevans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/davidevans/32/270050_2.png) [@davidevans](https://meta.discourse.org/u/davidevans)
#### Post date: [August 10, 2022, 2:55pm UTC](https://meta.discourse.org/t/generating-a-login-email-via-api/235737/1 "2022-08-10T14:55:20Z")

</div>

I’m looking to trigger the sending of a passwordless login email to a user from the API (that they would get if they hit ‘login by email’ themselves), and it doesn’t seem to be possible. I’ve used the ‘API reverse engineer’ technique to try and get at it, but authenticating with a global API key simply returns the homepage and triggers nothing (and without the api key it looks like it requires session cookies to be set etc.).

What I’m looking to achieve is a sign-up process with as few steps as possible; as users sign up through another system I create the users in Discourse, and I want them to get an email with the temporary login link to go straight in. This is all about reducing friction in a process that’s tedious enough!

Is there a trick to making this work through the API?

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [August 10, 2022, 4:10pm UTC](https://meta.discourse.org/t/generating-a-login-email-via-api/235737/2 "2022-08-10T16:10:02Z")

</div>

> [@davidevans](#):
>
> What I’m looking to achieve is a sign-up process with as few steps as possible; as users sign up through another system I create the users in Discourse,

In this case the proper solution would be a invisible login setup using [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true).

> [@davidevans](#):
>
> I’ve used the ‘API reverse engineer’ technique to try and get at it, but authenticating with a global API key simply returns the homepage and triggers nothing (and without the api key it looks like it requires session cookies to be set etc.).

Have you tried:

```plaintext
curl -X POST https://meta.discourse.org/u/email-login \
-H "Api-Key: longapikeyhere" \
-H "Api-Username: system" \
--data-raw 'login=email%40gmail.com'

```

?

---

<div class="post-metadata">

### Author: ![davidevans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/davidevans/32/270050_2.png) [@davidevans](https://meta.discourse.org/u/davidevans)
#### Post date: [August 10, 2022, 4:44pm UTC](https://meta.discourse.org/t/generating-a-login-email-via-api/235737/3 "2022-08-10T16:44:21Z")

</div>

Many thanks for the reply Falco. Specifically using curl exactly as you suggest I get a ‘You are being redirected’ response that would send me back to the home page. No login email is triggered. I was getting a similar result with the tool I was using - which was following the redirect back to the home page.

I don’t want to use [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) as the only required login after signup is for Discourse.

Just also to add that, as a long-time user (and admirer) of Discourse forums this is my first foray into the other side - and the API / webhooks are just great; really easy to work with and very powerful.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [August 10, 2022, 5:42pm UTC](https://meta.discourse.org/t/generating-a-login-email-via-api/235737/4 "2022-08-10T17:42:52Z")

</div>

> [@davidevans](#):
>
> I get a ‘You are being redirected’ response that would send me back to the home page.

That endpoint is not available for API calls at the moment indeed. When you pass API keys for it you fall on the redirect here:

> <https://github.com/discourse/discourse/blob/2e2fee9da3af7898d38e5a8797d5930d6c26fc03/app/controllers/users_controller.rb#L950>

And you can’t call it without API keys because this route is under the `:verify_authenticity_token` CSRF protection.

---

<div class="post-metadata">

### Author: ![davidevans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/davidevans/32/270050_2.png) [@davidevans](https://meta.discourse.org/u/davidevans)
#### Post date: [August 11, 2022, 8:58am UTC](https://meta.discourse.org/t/generating-a-login-email-via-api/235737/5 "2022-08-11T08:58:01Z")

</div>

Thanks - that’s clear, and I can see the logic to it. Really appreciate the replies.
