devhammed / byteship-php
PHP client for the Byteship Upload API.
Requires
- php: ^8.3
- graham-campbell/guzzle-factory: ^4.0.2|^5.0|^6.0|^7.0
- guzzlehttp/guzzle: ^6.2|^7.0
- illuminate/contracts: ^11.0||^12.0||^13.0
- league/flysystem: ^3.25.1
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
This package is auto-updated.
Last update: 2026-05-07 23:38:53 UTC
README
PHP client for the Byteship Upload API.
Installation
You can install the package via composer:
composer require devhammed/byteship-php
Usage
The first thing you need to do is to get an API key at Byteship. You'll find more info at the Byteship Docs.
Create a Client
use Devhammed\Byteship\Client; $client = new Client($_ENV['BYTESHIP_API_KEY']); $token = $client->createUploadToken( folder: "uploads", maxUploadBytes: 10 * 1024 * 1024, ); echo $token->uploadToken->token;
Upload a File
use Devhammed\Byteship\Client; use Devhammed\Byteship\Enums\Visibility; use Devhammed\Byteship\ValueObjects\UploadProgress; $client = new Client($_ENV['BYTESHIP_API_KEY']); $file = fopen('photo.jpg', 'rb'); $uploaded = $client->upload( $file, filename: "photo.jpg", contentType: "image/jpeg", path: "uploads/photo.jpg", visibility: Visibility::Public, onProgress: function (UploadProgress $progress) { echo round($progress->percent) . '% uploaded'; }, ); echo "#{$uploaded->id} - {$uploaded->url}";
Errors
use Devhammed\Byteship\Client; use Devhammed\Byteship\Error; $client = new Client($_ENV['BYTESHIP_API_KEY']); try { $client->createUploadToken( folder: "uploads", maxUploadBytes: 10 * 1024 * 1024, ); } catch (Error $error) { echo "Error creating upload token: {$error->getError()} - {$error->getStatus()} - {$error->getMessage()}"; }
You can check this folder for more usage examples.
Laravel
This package ships with a service provider for Laravel that will automatically setup the client for your application.
To get started, create an environment variable named BYTESHIP_API_KEY in your .env file with your Byteship API key:
BYTESHIP_API_KEY=your-api-key
Then, open config/filesystems.php and add the byteship disk configuration:
return [ // ... 'disks' => [ 'byteship' => [ 'driver' => 'byteship', 'visibility' => 'public', 'api_key' => env('BYTESHIP_API_KEY'), ], ], // ... ];
You should now be able to use the Byteship disk in your Laravel application just like any other storage drivers:
use Illuminate\Support\Facades\Storage; Storage::disk('byteship')->put('hello.txt', 'Hello, Byteship!'); // true/false Storage::disk('byteship')->get('hello.txt'); // "Hello, Byteship!" Storage::disk('byteship')->url('hello.txt'); // "https://cdn.byteship.dev/f/12345/hello.txt" (only for public files) Storage::disk('byteship')->temporaryUrl('hello.txt', now()->addHour()); // "https://cdn.byteship.dev/f/12345/hello.txt?token=secret-token" (only for private files) Storage::disk('byteship')->temporaryUploadUrl('hello.txt', now()->addHour(), ['byte_size' => 1024]) // ['file_id' => '123', 'upload_id' => '456', 'upload_token' => 'd34db33f', 'url' => 'https://...', 'complete_url' => 'https://...', 'headers' => ['content-type' => 'text/plain']] (use the complete URL + upload ID + upload_token to complete the upload after sending the file to the url + headers) Storage::disk('byteship')->delete('hello.txt'); // true/false Storage::disk('byteship')->exists('hello.txt'); // true/false Storage::disk('byteship')->mimeType('hello.txt'); // "text/plain" Storage::disk('byteship')->visibility('hello.txt'); // "public" / "private" Storage::disk('byteship')->size('hello.txt'); // 16
RECOMMENDED: You should set the default disk to
byteshipinsideconfig/filesystems.phpso you won't have to specify the disk name everytime you work with Storage.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.