# Migrate existing users

> Move users you already serve through your own app onto Koil, a cohort at a time, without anyone re-authorizing.

If your product already connects users through your own app, you can move them onto Koil without asking anyone to re-authorize: you keep the app, hand Koil the grants you hold, and route the app's webhooks through Koil while your existing system keeps receiving them. You can move users in cohorts, compare the two systems, and roll a cohort back.

Read [Bring your own app](https://docs.koil.co/byo-apps) and [Connect platform accounts](https://docs.koil.co/guides/connect-platform-accounts) first; this guide covers only what differs when the users already exist.

## Before you start

-   **Use the same app.** A platform refreshes a token only for the app that issued it, so register the app your grants came from (same client ID and secret) as the Auth Integration. Add Koil's redirect URI to the app _alongside_ your existing one; leave yours in place until you are done.
-   **Stop your old system from revoking credentials.** Koil and your old system hold the same grant, so a revoke on either side ends both. Turn off any revoke-on-disconnect, token cleanup, or scope change in your old system for the users you migrate.
-   **Check what your old system stored.** Each grant needs at least its access token. For a platform that issues refresh tokens (TikTok, Google, LinkedIn), import the refresh token and the access token's expiry too, or Koil cannot keep the grant alive. Where you stored the refresh token's own expiry (TikTok, LinkedIn), import it as `refreshTokenExpiresAt`, so Koil can warn before the user must connect again.

## 1\. Route webhooks through Koil

Meta (Facebook Pages, and Instagram through Facebook Login) and TikTok let an app send each kind of webhook to one URL only, so the app cannot notify Koil and your old system separately. Instead, Koil receives everything and copies it to your old endpoint:

1.  Set `webhookForwardUrl` on the Auth Integration to the URL your old system receives webhooks at.
2.  Point the app's webhooks at Koil, as [Bring your own app](https://docs.koil.co/byo-apps) describes.

Koil verifies each delivery and forwards every one of them, for migrated and unmigrated users alike, with the platform's original body and signature headers. Your old system verifies them with the app secret it already holds, so it keeps working unchanged. Copies are retried for about 20 minutes and can arrive more than once; a retried copy keeps the platform's original signature timestamp.

For TikTok, Koil registers the callback itself, and does not replace one your old system registered. While yours is in place, the Auth Integration's `webhookConflicts` lists the blocked event types; delete or repoint the callback with TikTok's webhook API, and Koil registers its own within a few minutes.

LinkedIn, YouTube, and Google Business Profile need nothing here: LinkedIn subscribes per Page and admin, so Koil moves only that admin's subscription for that Page, once an Event Destination covers the Profile (Koil forwards its notifications too), and YouTube and Google Business Profile are polled.

## 2\. Import a cohort

For each user in the cohort:

1.  Import the grant with `POST /v1/connections` (`method: "oauth"`). Use your own id for the user as `clientUserId`, and derive the `Idempotency-Key` from your own id for the grant (a user with grants on two platforms has two), so a rerun of your migration script never imports a grant twice. Koil checks Meta and TikTok grants with the platform before keeping them; a grant the platform rejects fails the import, and that user must connect again through a Connect Session.
2.  Create the Profile with `POST /v1/profiles`, passing the platform id of the account you already know as `platformProfileExternalId`; you do not need to list eligible profiles first. Derive its `Idempotency-Key` from your own id for the account, as for the import, and put your own ids in its `metadata`.

Each user costs two requests against your Organization's [rate limit](https://docs.koil.co/concepts/rate-limits), so pace the migration by the `RateLimit-*` headers, and back off on a `429` until `Retry-After`. From the moment a profile exists, Koil emits its events to your Event Destinations, while your old system keeps receiving the forwarded copies.

**TikTok grants change hands in one step.** TikTok may issue a new refresh token on every refresh, so whichever system refreshes first can invalidate the other's copy. Stop your old system refreshing a user's grant before you import it, and from then on read the current token from Koil if the old system still needs it (see [Leaving or rolling back](https://docs.koil.co/guides/migrate-existing-users#leaving-or-rolling-back)). Meta, Google, and LinkedIn grants are safe to hold in both systems during the migration: Meta's have no refresh token, and Google's and LinkedIn's do not change when used.

## 3\. Move the cohort over

Switch the cohort's reads, writes, and event handling to Koil behind a flag in your product, and compare against your old system, which still sees every webhook. To roll a cohort back, switch the flag back; nothing about the grants has to change, except TikTok's (above).

## 4\. Finish

Once every user is on Koil, clear `webhookForwardUrl` (set it to `null`) and retire your old system. You can then remove your old redirect URI from the app.

## Leaving or rolling back

`GET /v1/connections/{connectionId}/credential` returns an `oauth` Connection's current grant in the shape `POST /v1/connections` takes. It needs an API key created with the `koil:credentials:export` permission. Export the grant last, then delete the Connection, so Koil stops refreshing it.

## Known limits

-   A Google grant bound to your own key with DPoP cannot be used by Koil, since every refresh needs a proof from that key.
-   Instagram accounts connected through Instagram Login (rather than Facebook Login) cannot be imported yet.
