Blog Detail


How to implement Dropbox v2 API in your Php Applications cover image

arrow_back How to implement Dropbox v2 API in your Php Applications

Spatie comes up with a very handy package called Dropbox-API. It is a minimal PHP implementation of the Dropbox API v2. It contains only the methods needed for our flysystem-dropbox adapter.


You can install the package via composer by running this command:

composer require spatie/dropbox-api


The first thing you need to do is get an authorization token at Dropbox. Unlike other companies, Dropbox has made this very easy. You can just generate a token in the App Console for any Dropbox API app. You’ll find more info at the Dropbox Developer Blog.

With an authorization token you can instantiate a Spatie\Dropbox\Client.

$client = new Spatie\Dropbox\Client($authorizationToken);

or alternatively, you can implement Spatie\Dropbox\TokenProvider which will provide the access-token from its TokenProvider->getToken(): string method.

If you use oauth2 to authenticate and to acquire refresh-tokens and access-tokens, (like thephpleague/oauth2-client), you can create an adapter that internally takes care of token-expiration and refreshing tokens, and at runtime will supply the access-token via the TokenProvider->getToken(): string method.

(Dropbox announced they will be moving to short-lived access_tokens in 2021).

// implements Spatie\Dropbox\TokenProvider
$tokenProvider = new AutoRefreshingDropBoxTokenService($refreshToken);
$client = new Spatie\Dropbox\Client($tokenProvider);

or alternatively, you can authenticate as an App using your App Key & Secret.

$client = new Spatie\Dropbox\Client([$appKey, $appSecret]);

If you only need to access the public endpoints you can instantiate Spatie\Dropbox\Client without any arguments.

$client = new Spatie\Dropbox\Client();

Dropbox Endpoints

Look in the source code of Spatie\Dropbox\Client to discover the methods you can use.

Here’s an example:

$content = 'hello, world';
$client->upload('/dropboxpath/filename.txt', $content, $mode='add');

$from = '/dropboxpath/somefile.txt';
$to = '/dropboxpath/archive/somefile.txt';
$client->move($from, $to);

If the destination filename already exists, dropbox will throw an Exception with to/conflict/file/.

The upload() and move() methods have an optional extra autorename argument to try and let dropbox automatically rename the file if there is such a conflict.

Here’s an example:

$from = '/dropboxpath/somefile.txt';
$to = '/dropboxpath/archive/somefile.txt';
$client->move($from, $to, $autorename=true);
// with autorename results in 'somefile (1).txt'

If you do not find your favorite method, you can directly use the contentEndpointRequest and rpcEndpointRequest functions.

public function contentEndpointRequest(string $endpoint, array $arguments, $body): ResponseInterface

public function rpcEndpointRequest(string $endpoint, array $parameters): array

Here’s an example:

$client->rpcEndpointRequest('search', ['path' => '', 'query' => 'bat cave']);

If you need to change the subdomain of the endpoint URL used in the API request, you can prefix the endpoint path with a subdomain.

Here’s an example:

$client->rpcEndpointRequest('content::files/get_thumbnail_batch', $parameters);

This package is very handy for the implementation of Dropbox API, For more details & source code you can visit its complete documentation on Github.

Published at : 29-12-2021

Author : Rizwan Aslam
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project