How to Integrate DeepSeek API in Laravel

Ehtesham Ali - Feb 3 - - Dev Community

Prerequisites

Before we begin, ensure you have the following:

Laravel Installed: A working Laravel application (version 8.x or higher recommended).

DeepSeek API Key: Sign up for DeepSeek and obtain your API key from their dashboard.

Guzzle HTTP Client: Laravel uses Guzzle by default for making HTTP requests, so no additional installation is required.

Step 1: Set Up Your Laravel Project

If you haven’t already, create a new Laravel project:

composer create-project laravel/laravel deepseek-integration
cd deepseek-integration
Enter fullscreen mode Exit fullscreen mode

Step 2: Store Your DeepSeek API Key

It’s a good practice to store sensitive information like API keys in environment variables. Open your .env file and add your DeepSeek API key:

DEEPSEEK_API_KEY=your_api_key_here
Enter fullscreen mode Exit fullscreen mode

Then, add the key to your config/services.php file for easy access:

return [
    // Other services
    'deepseek' => [
        'api_key' => env('DEEPSEEK_API_KEY'),
    ],
];
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a Service Class

To keep your code clean and reusable, create a service class to handle interactions with the DeepSeek API.

Run the following Artisan command to generate the service class:

php artisan make:service DeepSeekService
Enter fullscreen mode Exit fullscreen mode

This will create a new file at app/Services/DeepSeekService.php. Open the file and add the following code:

<?php

namespace App\Services;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class DeepSeekService
{
    protected $client;
    protected $apiKey;

    public function __construct()
    {
        $this->client = new Client([
            'base_uri' => 'https://api.deepseek.com/v1/', // Replace with the actual DeepSeek API endpoint
        ]);
        $this->apiKey = config('services.deepseek.api_key');
    }

    /**
     * Make a request to the DeepSeek API.
     *
     * @param string $endpoint
     * @param array $data
     * @return array
     * @throws GuzzleException
     */
    public function makeRequest(string $endpoint, array $data = [])
    {
        $response = $this->client->post($endpoint, [
            'headers' => [
                'Authorization' => 'Bearer ' . $this->apiKey,
                'Content-Type' => 'application/json',
            ],
            'json' => $data,
        ]);

        return json_decode($response->getBody(), true);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Use the Service in a Controller

Now that the service is set up, let’s use it in a controller. Create a new controller using Artisan:

php artisan make:controller DeepSeekController
Enter fullscreen mode Exit fullscreen mode

Open the newly created controller at app/Http/Controllers/DeepSeekController.php and add the following code:

<?php

namespace App\Http\Controllers;

use App\Services\DeepSeekService;
use Illuminate\Http\Request;

class DeepSeekController extends Controller
{
    protected $deepSeekService;

    public function __construct(DeepSeekService $deepSeekService)
    {
        $this->deepSeekService = $deepSeekService;
    }

    public function analyzeData(Request $request)
    {
        // Example request data (replace with actual data you want to send to DeepSeek)
        $data = [
            'text' => $request->input('text'),
            'parameters' => [
                'language' => 'en',
                // Add other parameters as needed
            ],
        ];

        try {
            $response = $this->deepSeekService->makeRequest('analyze', $data); // Replace 'analyze' with the actual endpoint
            return response()->json($response);
        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Define a Route

Next, define a route to access the controller method. Open routes/web.php or routes/api.php and add the following:

use App\Http\Controllers\DeepSeekController;

Route::post('/analyze', [DeepSeekController::class, 'analyzeData']);
Enter fullscreen mode Exit fullscreen mode

Step 6: Test the Integration

You can now test the integration by sending a POST request to the /analyze endpoint. For example, using Postman or a frontend form:

{
    "text": "This is a sample text to analyze."
}
Enter fullscreen mode Exit fullscreen mode

If everything is set up correctly, you should receive a response from the DeepSeek API.

Conclusion

In this tutorial, we’ve successfully integrated the DeepSeek API into a Laravel application. By creating a service class and using Guzzle for HTTP requests, we’ve ensured that our code is clean, reusable, and easy to maintain. You can now extend this integration to include more features provided by DeepSeek.

Feel free to explore the DeepSeek API documentation for more advanced use cases and endpoints.

Happy coding! 🚀

clapping

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .