spatie/dropbox-api
Minimal PHP client for Dropbox API v2 by Spatie. Provides core endpoints used by their Flysystem Dropbox adapter—create folders, list directories, fetch temporary links, and more. Easy to install via Composer and use with an auth token.
Installation:
composer require spatie/dropbox-api
Register the service provider in config/app.php (if not auto-discovered):
Spatie\Dropbox\DropboxServiceProvider::class,
First Use Case: Authenticate and list files in a root folder:
use Spatie\Dropbox\Client;
$client = new Client(config('services.dropbox.token'));
$files = $client->listFolder('/');
Configuration:
Add your Dropbox OAuth2 token to .env:
DROPBOX_TOKEN=your_access_token_here
Publish the config file (optional):
php artisan vendor:publish --provider="Spatie\Dropbox\DropboxServiceProvider"
File Operations:
uploadFile()/downloadFile() with local paths or streams.$client->uploadFile('/local/path.txt', '/remote/path.txt');
$client->downloadFile('/remote/path.txt', '/local/download.txt');
Folder Management:
createFolder().deleteFolder() (empty only).$client->createFolder('/projects/2023');
$client->deleteFolder('/projects/2022');
Metadata & Sharing:
getMetadata().getTemporaryLink() or getPermanentLink().$metadata = $client->getMetadata('/file.txt');
$link = $client->getTemporaryLink('/file.txt', 3600); // Expires in 1 hour
Large File Handling:
uploadFileWithChunking() for files > 150MB.$client->uploadFileWithChunking('/large/video.mp4', '/remote/video.mp4');
Laravel Filesystem:
Combine with spatie/flysystem-dropbox for seamless integration:
use Spatie\Dropbox\DropboxFilesystem;
$filesystem = new DropboxFilesystem($client);
$filesystem->put('remote-file.txt', 'content');
Queue Jobs: Offload long-running operations (e.g., large uploads) to queues:
UploadDropboxFile::dispatch($localPath, $remotePath)->onQueue('dropbox');
Event Listeners: Trigger events post-upload/download (e.g., notifications):
$client->uploadFile(...);
event(new FileUploaded($remotePath));
Token Management:
.env or a secure secrets manager.Rate Limits:
Path Handling:
/) separated, even on Windows./folder/ vs /folder).Large File Quirks:
Permissions:
app_folder).SPATIE_DROPBOX_DEBUG=true in .env to log raw API responses.401 Unauthorized: Invalid/expired token.403 Forbidden: Insufficient permissions.404 Not Found: Path does not exist.Custom Endpoints:
Extend the Client class to add unsupported API methods:
class CustomClient extends Client {
public function customMethod() {
return $this->request('POST', '/custom-endpoint', []);
}
}
Webhooks: Use Dropbox’s webhook events to trigger Laravel jobs:
Route::post('/dropbox/webhook', function (Request $request) {
event(new DropboxWebhookReceived($request->input()));
});
Testing: Mock the client for unit tests:
$mockClient = Mockery::mock(Spatie\Dropbox\Client::class);
$mockClient->shouldReceive('listFolder')->andReturn(['files' => []]);
listFolderContinue for paginated results (handled automatically by the package).$metadata = Cache::remember("dropbox:metadata:{$path}", now()->addHours(1), function () use ($client, $path) {
return $client->getMetadata($path);
});
How can I help you explore Laravel packages today?