Laravel Forge API is a powerful tool that allows you to manage your servers and applications through a RESTful API provided by Laravel Forge. Laravel Forge is a server management and deployment service for PHP applications that simplifies the process of provisioning and managing web servers. With the Forge API, developers can automate and manage their server infrastructure programmatically.
Key Features
- Server Management: Create, update, and delete servers directly through the API.
- Site Management: Manage your applications and sites hosted on your servers.
- SSL Management: Easily handle SSL certificates for your applications.
- Environment Variables: Manage environment variables for your applications.
- Database Management: Create and manage databases for your applications.
Getting Started
1. Setup Laravel Forge
Before you can use the Laravel Forge API, you need to have a Laravel Forge account and at least one server set up.
2. Obtain an API Token
To authenticate requests to the Laravel Forge API, you need to generate an API token:
- Log in to your Laravel Forge account.
- Navigate to your account settings.
- Generate a new API token and keep it secure.
3. Making API Requests
You can use tools like Postman or any HTTP client (e.g., Guzzle) to interact with the Forge API. Here’s how to make basic API requests:
Example: Fetching All Servers
You can retrieve a list of all your servers using a GET request:
1 2 |
GET https://forge.laravel.com/api/v1/servers Authorization: Bearer YOUR_API_TOKEN |
Example Code Using Guzzle
Here’s an example of how to use Guzzle to fetch all servers:
1 2 3 4 5 6 7 8 9 10 11 |
use GuzzleHttp\Client; $client = new Client(); $response = $client->request('GET', 'https://forge.laravel.com/api/v1/servers', [ 'headers' => [ 'Authorization' => 'Bearer YOUR_API_TOKEN', ], ]); $servers = json_decode($response->getBody(), true); print_r($servers); |
Common API Endpoints
Here are some common endpoints you might use with the Laravel Forge API:
- List Servers:
GET /api/v1/servers
- Create a New Server:
POST /api/v1/servers
- Payload: You will need to provide details like the server name, provider, region, etc.
- Delete a Server:
DELETE /api/v1/servers/{serverId}
- Manage Sites:
- List all sites on a server:
GET /api/v1/servers/{serverId}/sites
- Create a site:
POST /api/v1/servers/{serverId}/sites
- Delete a site:
DELETE /api/v1/servers/{serverId}/sites/{siteId}
- List all sites on a server:
- Manage SSL Certificates:
- Add SSL:
POST /api/v1/servers/{serverId}/sites/{siteId}/ssl
- Remove SSL:
DELETE /api/v1/servers/{serverId}/sites/{siteId}/ssl
- Add SSL:
- Environment Variables:
- List environment variables:
GET /api/v1/servers/{serverId}/sites/{siteId}/env
- Update environment variables:
PUT /api/v1/servers/{serverId}/sites/{siteId}/env
- List environment variables:
Example: Creating a New Server
To create a new server, you can send a POST request:
1 2 3 4 5 6 7 8 9 10 11 12 |
$response = $client->request('POST', 'https://forge.laravel.com/api/v1/servers', [ 'headers' => [ 'Authorization' => 'Bearer YOUR_API_TOKEN', ], 'json' => [ 'name' => 'New Server', 'provider' => 'digitalocean', 'size' => 's-1vcpu-1gb', 'region' => 'nyc1', // Add any other required parameters... ], ]); |
Conclusion
The Laravel Forge API provides a powerful way to manage your servers programmatically, making it easier to integrate server management into your development workflows. Whether you’re automating deployments, managing SSL certificates, or handling environment variables, the Forge API simplifies the process.