> 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tryterra.co/faq/help-topics/data-api-sdk/authentication-users-and-connection-state/detect-existing-connection-disconnect.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
