Search code examples
slackslack-api

Slack API returns invalid_email error for valid email address


I am sending API request to undocumented API users.admin.invite to invite new team members to organization programatically. But every time I send the request, it results in error invalid_email. Which is weird, because if I fire the same request from https://{myorg}.slack.com/admin/invites it works.

I've managed to obtain the again undocumented client scope token, which is required for this. I've changed to content type to multipart/form-data, which seems to be required. But still it does not work..

The relevant code looks like this

const options = {
            host: 'myOrg.slack.com',
            path: `/api/users.admin.invite`,
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${config.slackAuthToken}`,
                'Content-Type': 'multipart/form-data',
                'Accept-Language': 'cs,en-GB;q=0.9,en;q=0.8',
                'Accept': 'application/json',
                'Cache-Control': 'no-cache'
            },
            formData: {
                "email": event.email,
                "first_name": event.firstName,
                "last_name": event.lastName,
                "real_name": `${event.firstName} ${event.lastName}`,
                 "set_active": true,
                 "resend": true, // Resend the invitation email if the user has already been invited and the email was sent some time ago
            }

 https.request(options) // simplified

Solution

  • It is a known bug of the undocumented API method admin.users.invite that some emails won't work (its also mentioned in the docs). Nothing much you can do about it I am afraid.

    UPDATE

    The problem was that the request must be send as application/x-www-form-urlencoded