Making your first API request
Let’s install the API library using a package manager (you can also download the source yourself from the links in the code blocks):
# We strongly recommend using Composer, but if you'd prefer to # install the PHP library manually, see the instructions at # https://github.com/gocardless/gocardless-pro-php#manual-installation composer require gocardless/gocardless-pro
Our API libraries have
pro in their names, but don’t worry: if you’re starting a new integration, these are the right libraries for you, whichever GoCardless package you’re using.
Now we can import the library into our code so that it’s ready to use:
To start using the API, you’ll need an access token.
First, sign up for an account in our sandbox: the sandbox is our dedicated testing environment where you can build and test your integration without touching real money.
Next, create an access token. Head to https://manage-sandbox.gocardless.com/developers/access-tokens/create, then give your access token a name and make sure you give it read-write access. Click “Create access token”, and copy the token to your clipboard.
You’re now ready to initialise the client:
1<?php 2require 'vendor/autoload.php'; 3 4$client = new \GoCardlessPro\Client([ 5 // We recommend storing your access token in an 6 // environment variable for security 7 'access_token' => getenv('GC_ACCESS_TOKEN'), 8 // Change me to LIVE when you're ready to go live 9 'environment' => \GoCardlessPro\Environment::SANDBOX 10]);
Now, let’s check that the client is working by making our first request to the API: List customers
1$customers = $client->customers()->list()->records; 2print_r($customers);
Run that code and you should get an empty array, as you haven’t added any customers yet - but the fact that you got a response back shows that you’re authenticated and ready to go.
1Array 2( 3)