Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CrastyCrap's avatar

No Api Key Provided

i am trying to use laravel cashier but i get the following error

No API key provided. Set your API key when constructing the StripeClient instance, or provide it on a per-request basis using the `api_key` key in the $opts argument

My User Model

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable,MailerService,Billable;
}
public function register(array $request)
    {
        // Create the company
        $company = self::create([
            'name'  => $request['name'],
            'email' => $request['email'],
            'country' => $request['country'],
            'address' => $request['address'],
            'description' => $request['description'] ?? null,
            'seats' => $request['seats'],
            'active' => false
        ]);

        // Create main root user
        $request['user']['type'] = 2;
        $rootUser = User::createInstance($request['user']);
        $rootUserLogin = $rootUser->login($rootUser->email,$request['user']['password']);

        // Payment intent
        $payment = $rootUser->payWith('20',['card']);

        return [
            'company' => $company,
            'token'   => $rootUserLogin['token'],
            'user'    => $rootUser 
        ];
    }

my .env

STRIPE_KEY="pk_mykey"
STRIPE_SECRET_KEY="sk_test_mysec"
0 likes
1 reply
LaryAI's avatar
Level 58

The error message suggests that the Stripe API key is not being provided. The solution is to ensure that the API key is set correctly.

In this case, the .env file seems to have the correct keys set. However, the code does not seem to be using the Stripe API key.

To use the Stripe API key, you need to pass it to the StripeClient instance. In Laravel Cashier, this is done automatically when you use the Billable trait.

In the code provided, the Billable trait is being used, but the payWith method is not passing the Stripe API key.

To fix this, you can pass the Stripe API key as the second argument to the payWith method.

For example:

$payment = $rootUser->payWith('20', ['card'], $this->stripeOptions());

Here, the stripeOptions method returns an array with the api_key key set to the Stripe API key.

You can define the stripeOptions method in the User model like this:

public function stripeOptions()
{
    return [
        'api_key' => config('services.stripe.secret'),
    ];
}

This method returns an array with the api_key key set to the Stripe API key from the .env file.

Make sure to import the config helper at the top of the file:

use Illuminate\Support\Facades\Config;

With this change, the payWith method should be able to use the Stripe API key and the error should be resolved.

Please or to participate in this conversation.