Get external_id used in SSO through the API

AI-generated summary

In a support discussion titled ‘Get external_id used in SSO through the API’, henriqueberlesi seeks to retrieve the external_id from SSO via the Discourse API to match forum sign-ups with app sign-ups. They note the absence of this field in the standard user JSON response and mention that the single_sign_on_record is not documented in the API reference. simon advises using the /admin/users/{user_id}.json endpoint, which includes the single_sign_on_record field. henriqueberlesi confirms they were querying the regular user route instead, and after following simon’s suggestion — navigating to the admin user page and appending .json to the URL — successfully retrieved the needed data. They thank simon for the solution.

We are trying to match the sign ups in our forum with our app sign ups.

In my opinion, it seems easier to populate a database when the SSO happens, but this wasn’t implemented yet.

So, is it possible to get the user’s external_id used in SSO through the Discourse API?

Here is a standard Json for a random user:

"user": {
    "moderator": false,
    "can_change_bio": false,
    "primary_group_flair_bg_color": null,
    "uploaded_avatar_id": null, 
    "title": null,
    "watched_category_ids": [],
    "trust_level": 0,
    "id": 1234,
    "custom_avatar_template": null,
    "muted_category_ids": [],
    "watching_first_post_tags": [],
    "featured_user_badge_ids": [],
    "tracked_category_ids": [],
    "watched_tags": [],
    "mailing_list_posts_per_day": 1,
    "locale": null,
    "watched_first_post_category_ids": [],
    "recent_time_read": 0,
    "primary_group_flair_url": null,
    "user_option": {
        "user_id": 1234,
        "email_digests": true,
        "external_links_in_new_tab": false,
        "like_notification_frequency": 1,
        "enable_quoting": true,
        "mailing_list_mode": false,
        "mailing_list_mode_frequency": 1,
        "email_direct": true,
        "theme_key_seq": 0,
        "theme_key": theme_key,
        "new_topic_duration_minutes": 2880,
        "disable_jump_reply": false,
        "include_tl0_in_digests": false,
        "dynamic_favicon": false,
        "email_always": false,
        "automatically_unpin_topics": true,
        "email_in_reply_to": true,
        "homepage_id": null,
        "allow_private_messages": true,
        "email_private_messages": true,
        "digest_after_minutes": 10080,
        "email_previous_replies": 2,
        "auto_track_topics_after_msecs": 240000,
        "notification_level_when_replying": 2
    },
    "groups": [
        {
            "name": group_name,
            "display_name": display_name
            "automatic_membership_retroactive": false,
            "has_messages": false,
            "bio_raw": null,
            "messageable_level": 0,
            "flair_bg_color": null,
            "incoming_email": null,
            "visibility_level": 0,
            "flair_url": null,
            "id": 1,
            "automatic": true,
            "full_name": null,
            "mentionable_level": 0,
            "public_exit": false,
            "primary_group": false,
            "flair_color": null,
            "grant_trust_level": null,
            "bio_cooked": null,
            "automatic_membership_email_domains": null,
            "membership_request_template": null,
            "public_admission": false,
            "allow_membership_requests": false,
            "title": null,
            "user_count": 1234,
            "default_notification_level": 3
        }
    ],
    "badge_count": 0,
    "can_edit_username": true,
    "can_edit_name": true,
    "gravatar_avatar_template": null,
    "admin": false,
    "last_posted_at": null,
    "can_send_private_messages": true,
    "can_edit_email": true,
    "user_api_keys": null,
    "name": name,
    "profile_view_count": 0,
    "time_read": 0,
    "custom_fields": {},
    "staged": false,
    "created_at": created_at,
    "second_factor_enabled": false,
    "primary_group_flair_color": null,
    "invited_by": null,
    "can_be_deleted": true,
    "primary_group_name": null,
    "system_avatar_upload_id": null,
    "last_seen_at": null,
    "username": username,
    "custom_avatar_upload_id": null,
    "post_count": 0,
    "user_fields": {
        "1": null
    },
    "can_delete_all_posts": true,
    "can_edit": true,
    "has_title_badges": false,
    "card_badge": null,
    "muted_usernames": [],
    "muted_tags": [],
    "system_avatar_template": "",
    "group_users": [
        {
            "user_id": 1234,
            "notification_level": 3,
            "group_id": 1
        }
    ],
    "tracked_tags": [],
    "gravatar_avatar_upload_id": null,
    "avatar_template": "",
    "can_send_private_message_to_user": true,
    "pending_count": 0
}

The /admin/users/{user_id}.json route returns the single_sign_on_record.

That’s exactly the endpoint I’m sending the request but I can’t find the field single_sign_on_record.

Also I believe that this is not documented in the API reference right? But I’m getting other fields like user_options that doesn’t show in the reference either.

If it helps, this is a standard Json from a random user:

"user_badges": [],
"user": {
    "moderator": false,
    "can_change_bio": false,
    "primary_group_flair_bg_color": null,
    "uploaded_avatar_id": null, 
    "title": null,
    "watched_category_ids": [],
    "trust_level": 0,
    "id": 1234,
    "custom_avatar_template": null,
    "muted_category_ids": [],
    "watching_first_post_tags": [],
    "featured_user_badge_ids": [],
    "tracked_category_ids": [],
    "watched_tags": [],
    "mailing_list_posts_per_day": 1,
    "locale": null,
    "watched_first_post_category_ids": [],
    "recent_time_read": 0,
    "primary_group_flair_url": null,
    "user_option": {
        "user_id": 1234,
        "email_digests": true,
        "external_links_in_new_tab": false,
        "like_notification_frequency": 1,
        "enable_quoting": true,
        "mailing_list_mode": false,
        "mailing_list_mode_frequency": 1,
        "email_direct": true,
        "theme_key_seq": 0,
        "theme_key": theme_key,
        "new_topic_duration_minutes": 2880,
        "disable_jump_reply": false,
        "include_tl0_in_digests": false,
        "dynamic_favicon": false,
        "email_always": false,
        "automatically_unpin_topics": true,
        "email_in_reply_to": true,
        "homepage_id": null,
        "allow_private_messages": true,
        "email_private_messages": true,
        "digest_after_minutes": 10080,
        "email_previous_replies": 2,
        "auto_track_topics_after_msecs": 240000,
        "notification_level_when_replying": 2
    },
    "groups": [
        {
            "name": group_name,
            "display_name": display_name
            "automatic_membership_retroactive": false,
            "has_messages": false,
            "bio_raw": null,
            "messageable_level": 0,
            "flair_bg_color": null,
            "incoming_email": null,
            "visibility_level": 0,
            "flair_url": null,
            "id": 1,
            "automatic": true,
            "full_name": null,
            "mentionable_level": 0,
            "public_exit": false,
            "primary_group": false,
            "flair_color": null,
            "grant_trust_level": null,
            "bio_cooked": null,
            "automatic_membership_email_domains": null,
            "membership_request_template": null,
            "public_admission": false,
            "allow_membership_requests": false,
            "title": null,
            "user_count": 1234,
            "default_notification_level": 3
        }
    ],
    "badge_count": 0,
    "can_edit_username": true,
    "can_edit_name": true,
    "gravatar_avatar_template": null,
    "admin": false,
    "last_posted_at": null,
    "can_send_private_messages": true,
    "can_edit_email": true,
    "user_api_keys": null,
    "name": name,
    "profile_view_count": 0,
    "time_read": 0,
    "custom_fields": {},
    "staged": false,
    "created_at": created_at,
    "second_factor_enabled": false,
    "primary_group_flair_color": null,
    "invited_by": null,
    "can_be_deleted": true,
    "primary_group_name": null,
    "system_avatar_upload_id": null,
    "last_seen_at": null,
    "username": username,
    "custom_avatar_upload_id": null,
    "post_count": 0,
    "user_fields": {
        "1": null
    },
    "can_delete_all_posts": true,
    "can_edit": true,
    "has_title_badges": false,
    "card_badge": null,
    "muted_usernames": [],
    "muted_tags": [],
    "system_avatar_template": "",
    "group_users": [
        {
            "user_id": 1234,
            "notification_level": 3,
            "group_id": 1
        }
    ],
    "tracked_tags": [],
    "gravatar_avatar_upload_id": null,
    "avatar_template": "",
    "can_send_private_message_to_user": true,
    "pending_count": 0
}````

That looks like the data that’s returned from the regular user route, not the /admin/users/{user_id}.json route. To double check, go to the /admin/users page on your forum. Click on the username of a user you know has logged in through SSO. On that user’s page, add .json to the URL in your browser’s address bar. You should see a json response with a single_sign_on_record property.

Oh, I see!

Using this method I was able to retrieve the proper user information that I was looking for.

Thanks for your help, @simon!