> ## Documentation Index
> Fetch the complete documentation index at: https://dylankenneally-react-native-ssh-sftp-96.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# SFTP Connection

> Connect and disconnect from SFTP servers.

export const EditThisPage = ({filePath}) => {
  const REPO_EDIT_BASE_URL = 'https://github.com/dylankenneally/react-native-ssh-sftp/edit/master';
  return <div className="mt-10 rounded-2xl border border-zinc-950/10 bg-zinc-50 px-4 py-3 dark:border-white/10 dark:bg-white/5">
      <p className="m-0 text-sm text-zinc-600 dark:text-zinc-300">
        See something to improve?{' '}
        <a href={`${REPO_EDIT_BASE_URL}/${filePath}`}>Edit this page on GitHub</a>{' '}
        to suggest a change.
      </p>
    </div>;
};

## connectSFTP()

Connects to the SFTP server.

<Info>
  It is not mandatory to call this method before calling any SFTP method. The library will automatically establish an SFTP connection when needed.
</Info>

### Signature

```typescript theme={null}
connectSFTP(callback?: CallbackFunction<void>): Promise<void>
```

### Parameters

<ParamField path="callback" type="CallbackFunction<void>" optional>
  Optional callback function to be called after the connection is established.

  ```typescript theme={null}
  (error: any, response?: void) => void
  ```
</ParamField>

### Returns

<ResponseField name="Promise" type="Promise<void>">
  A promise that resolves when the connection is established successfully, or rejects with an error if the connection fails.
</ResponseField>

### Example

```typescript theme={null}
import SSHClient from '@dylankenneally/react-native-ssh-sftp';

// Using Promise
await client.connectSFTP();
console.log('SFTP connected');

// Using callback
client.connectSFTP((error) => {
  if (error) {
    console.error('SFTP connection failed:', error);
    return;
  }
  console.log('SFTP connected');
});
```

***

## disconnectSFTP()

Disconnects the SFTP connection.

<Warning>
  On iOS, this method has limited functionality. The SFTP stream will be closed when the SSH connection is disconnected using `disconnect()`.
</Warning>

### Signature

```typescript theme={null}
disconnectSFTP(): void
```

### Parameters

None

### Returns

<ResponseField name="void" type="void">
  This method does not return a value.
</ResponseField>

### Example

```typescript theme={null}
import SSHClient from '@dylankenneally/react-native-ssh-sftp';

// Disconnect SFTP
client.disconnectSFTP();
console.log('SFTP disconnected');
```

### Platform Notes

* **Android**: Fully supported. Explicitly closes the SFTP channel.
* **iOS**: The SFTP stream will be closed when `disconnect()` is called on the SSH client.

<EditThisPage filePath="docs/api/sftp/connect.mdx" />
