> For the complete documentation index, see [llms.txt](https://docs.tryterra.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tryterra.co/faq/help-topics/data-api-sdk/authentication-users-and-connection-state/detect-existing-connection-disconnect.md).

# How do I detect an existing native SDK connection?

**Use the mobile SDK's `getUserId` function for the connection type to detect an existing native connection.** It returns the existing `user_id` if one is present.

With that `user_id` you can either:

* Restore the record in your own database, or
* Call the `/auth/deauthenticateUser` endpoint to properly disconnect.

Running this check at SDK initialisation handles the "already connected but metadata missing" case automatically.

The SDK uses the device's unique OS identifier to maintain the connection, so a reinstall on the same device should resolve to the same `user_id` unless the user was deauthenticated. (`reference_id` is your own mapping attribute and does not by itself re-attach a connection.)

If new `user_id`s appear on reinstall, upgrade to the latest [SDK](https://docs.tryterra.co/health-and-fitness-api/mobile-only-sources) and store the `user_id` on your backend keyed by your own user identifier as a safeguard.
