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

CanadianDeer's avatar

set your API key using "Stripe::setApiKey(<API-KEY>)"

Hello,

I have this error message :

No API key provided. (HINT: set your API key using "Stripe::setApiKey(<API-KEY>)". You can generate API keys from the Stripe web interface. See https://stripe.com/api for details, or email [email protected] if you have any questions.

This is my code:

public function createStandardPlan(User $user, Request $request)
    {
        Stripe::setApiKey(env('STRIPE_SECRET'));

        return with($request->user(), function($user) use ($request) {
            return with(CheckoutSession::create(array_merge($user->stripe_id ? ['customer' => $user->stripe_id] : ['customer_email' => $user->email], [
                'payment_method_types'  => ['card'],
                'subscription_data' => [
                    'items'             => [['plan' => 'price_id']],
                ],
                'mode'                  => 'subscription',
                'client_reference_id'   => $user->id,
                'success_url'           => url('/dashboard'),
                'cancel_url'            => URL::previous(url('/dashboard/create-subscription')),
            ])), fn ($session) => $session->id);
        });
    }

The setApiKey is good no ? I make php artisan config:cache and route:cache but it's same.

Thank you

0 likes
10 replies
tykus's avatar

I wonder if the issue is where / when you are using the API key elsewhere - if it is set in this action, is it actually expected in another? You should be able to safely set the API key in the boot method of a ServiceProvider (e.g. AppServiceProvider) so if is available in all requests.

Aside, you should not use env() except in the config/*.php files.

CanadianDeer's avatar

@tykus in my config/services.php I have this :

'stripe' => [
        'model'  => env('CASHIER_MODEL'),
        'key'    => env('STRIPE_KEY'),
        'secret_key' => env('STRIPE_API_SECRET'),
    ],

Maybe this, but how ?

tykus's avatar

If you have a different env key in the configuration lie compared with the controller action.

In any case, use the config helper to get the correct key for Stripe

config(‘services.stripe.key’)
tykus's avatar

Does the config helper give you the correct value?

tykus's avatar
tykus
Best Answer
Level 104

But I asked if the config helper returned the correct value; you have not proven this yet AFAIK.

Also, if the secret key is needed, then

Stripe::setApiKey(config(‘services.stripe.secret_key’))
tykus's avatar

dd(config(‘services.stripe.secret_key’))

tykus's avatar

If you have cached your config, then clear that cache

Please or to participate in this conversation.