> ## Documentation Index
> Fetch the complete documentation index at: https://aftercaredocs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Google Drive Integration: Import and Back Up Documents

> Connect Google Drive to import discharge documents and back up your recovery guide. Complete the OAuth flow before calling the import or backup endpoints.

AfterCare's Google Drive integration lets you pull discharge paperwork directly from your Drive and push your generated recovery guide back as a JSON backup. The integration uses Google's `drive.file` OAuth scope, which grants AfterCare access only to files it creates or that the user explicitly selects — no broader access to your Drive. Follow the steps below to connect Drive before calling the import or backup endpoints.

<Warning>
  The `POST /drive/import` and `POST /drive/backup` endpoints currently return **503 Service Unavailable** until the Google OAuth configuration is fully provisioned in this environment. Check `GET /drive/status` to confirm the integration is active before calling them.
</Warning>

***

## Connect Google Drive

<Steps>
  ### Check the current integration status

  Call `GET /drive/status` to confirm whether Drive is already connected for the authenticated user. If it is, you can skip the OAuth flow and go straight to importing or backing up.

  ```bash cURL theme={null}
  curl -X GET https://api.aftercare.app/drive/status \
    -H "Authorization: Bearer <your_access_token>"
  ```

  A response with `"connected": true` means the OAuth token is present and Drive is ready to use.

  ### Start the OAuth flow

  If Drive is not connected, call `POST /drive/auth` to generate the Google authorization URL. Redirect the user's browser to the URL returned in the response to begin the limited `drive.file` OAuth consent flow.

  ```bash cURL theme={null}
  curl -X POST https://api.aftercare.app/drive/auth \
    -H "Authorization: Bearer <your_access_token>"
  ```

  The response contains the authorization URL and a signed `state` token that ties the OAuth callback back to the user's session. Open the URL in the user's browser so they can grant consent on Google's consent screen.

  ### Complete the OAuth callback

  After the user grants consent, Google redirects their browser to `GET /drive/callback` with a `code` and the original `state` parameter. AfterCare's server handles this redirect automatically — you do not call this endpoint yourself.

  <Note>
    `GET /drive/callback` is the OAuth redirect URI registered with Google. Your application never calls it directly. Once AfterCare receives the callback, it exchanges the authorization code for an access token and marks the Drive connection as active for the authenticated user.
  </Note>

  ### Import or back up

  With Drive connected, call `POST /drive/import` to pull a file into AfterCare for processing, or `POST /drive/backup` to save a recovery guide back to Drive.
</Steps>

***

## Endpoint reference

### GET /drive/status

Return the current Google Drive connection status for the authenticated user.

```http theme={null}
GET /drive/status
```

```bash cURL theme={null}
curl -X GET https://api.aftercare.app/drive/status \
  -H "Authorization: Bearer <your_access_token>"
```

#### Response — 200 OK

```json Example response theme={null}
{
  "connected": false
}
```

***

### POST /drive/auth

Start the `drive.file` OAuth authorization flow and return the Google consent URL.

```http theme={null}
POST /drive/auth
```

```bash cURL theme={null}
curl -X POST https://api.aftercare.app/drive/auth \
  -H "Authorization: Bearer <your_access_token>"
```

#### Response — 200 OK

```json Example response theme={null}
{
  "authUrl": "https://accounts.google.com/o/oauth2/v2/auth?...",
  "state": "eyJhbGciOiJIUzI1NiJ9..."
}
```

Redirect the user to `authUrl` to show the Google consent screen. The `state` value is signed by AfterCare and validated when the callback arrives — do not modify it.

***

### GET /drive/callback

Complete the OAuth flow after Google redirects the user back with an authorization code. AfterCare calls this endpoint internally as part of the redirect — your app does not call it directly.

```http theme={null}
GET /drive/callback
```

<Note>
  `GET /drive/callback` is AfterCare's OAuth redirect URI — your application never calls it directly. Once Google redirects the user here, AfterCare exchanges the authorization code for an access token and marks Drive as connected. The `state` parameter is validated on every callback to prevent CSRF attacks.
</Note>

#### Query parameters

| Parameter | Type | Description |
| - | - | - |
| `code` | string | The authorization code returned by Google. |
| `state` | string | The signed state token issued by `POST /drive/auth`. |

***

### POST /drive/import

Import a file from the connected Google Drive and queue it for processing as a discharge document. If the same file has been imported before, the endpoint returns the existing document instead of creating a duplicate.

```http theme={null}
POST /drive/import
```

<ParamField body="fileId" type="string" required>
  The Google Drive file ID of the document to import. The file must be accessible to the OAuth token on record for the authenticated user.
</ParamField>

#### Response — 202 Accepted

```json New document theme={null}
{
  "documentId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
  "status": "processing",
  "deduplicated": false
}
```

```json Duplicate detected theme={null}
{
  "documentId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
  "status": "ready",
  "deduplicated": true
}
```

When `deduplicated` is `true`, the file was already imported and the existing document is returned. No new processing is queued.

***

### POST /drive/backup

Back up a recovery guide to the connected Google Drive as a formatted JSON file named `recovery-guide-{documentId}.json`.

```http theme={null}
POST /drive/backup
```

<ParamField body="documentId" type="string (UUID)" required>
  The UUID of the document whose recovery plan you want to back up. The document must belong to the authenticated user and must have a completed recovery plan (`status: "ready"`).
</ParamField>

#### Response — 201 Created

```json Example response theme={null}
{
  "fileId": "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgVE2upms",
  "fileName": "recovery-guide-3fa85f64-5717-4562-b3fc-2c963f66afa6.json"
}
```

#### Response — 404 Not Found

```json theme={null}
{ "error": "Recovery plan not found" }
```

Returned when the document does not exist, does not belong to the authenticated user, or does not yet have a completed recovery plan.

<CardGroup cols={2}>
  <Card title="Upload documents" icon="upload" href="/api/upload">
    Upload a discharge document directly from your device instead of importing from Drive.
  </Card>

  <Card title="Ask a question" icon="message-question" href="/api/ask">
    Ask a natural-language question grounded in any uploaded or imported document.
  </Card>
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.