Integrate Plaid Bank Verification in Python Django: Step-by-Step Guide

Written by

in

Integrating bank account verification into your Python Django backend can significantly enhance the trust and security of your application. Plaid, a leading financial technology company, provides robust tools for verifying bank accounts. Here’s a step-by-step guide on how to integrate Plaid’s bank account verification into your Django backend.

Before we dive in, ensure you have a Plaid account and have obtained your Public Key and Secret Key. If not, you can sign up at Plaid’s website.

Setting Up Your Django Project

First, install Plaid’s Django library using pip:

pip install django-plaid

Next, add ‘plaid’ to your INSTALLED_APPS in Django’s settings.py file:

INSTALLED_APPS = [ ... 'plaid', ]

Creating a Plaid Client

In your Django view or function where you want to initiate the bank account verification process, create a Plaid client with your Public Key:

from plaid import Client

PLAID_PUBLIC_KEY = 'your_public_key'

client = Client(PLAID_PUBLIC_KEY)

Replace ‘your_public_key’ with your actual Plaid Public Key.

Initiating the Link Token Creation

To initiate the bank account verification process, you need to create a Link token. This token will be passed to your frontend to initiate the Plaid Link flow:

link_token = client.LinkToken.create(
user=plaid_user_id, # replace with user's ID
client_name="Your Company Name",
products=["transactions"],
country_codes=["US"],
language="en",
webhook="https://your-django-backend.com/webhook", # replace with your webhook URL
)

Send the ‘link_token’ to your frontend, and Plaid Link will handle the rest.

Handling the Public Token

After the user successfully connects their bank account, Plaid will send a ‘public_token’ to your backend. You should store this token for future use:

public_token = request.POST.get('public_token', None)

if public_token is not None:
exchange_response = client.Item.public_token.exchange(public_token)
your_item_id = exchange_response['item_id']
# save 'your_item_id' and 'public_token' for future use

Getting Account Information

With the item ID, you can fetch account information using the /accounts/get endpoint:

accounts_response = client.Accounts.get(your_item_id)

for account in accounts_response['accounts']:
account_id = account['account_id']
official_name = account['official_name']
balances = account['balances']
# process account information

This will provide you with the verified bank account details.

Storing Account Information

You can store the account information in your Django models for future use. Create a Django model to store the bank account details:

from django.db import models

class BankAccount(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
account_id = models.CharField(max_length=50)
official_name = models.CharField(max_length=100)
balances = models.JSONField()

def __str__(self):
return f"{self.user.username}'s Bank Account: {self.official_name}"

Then, save the account information whenever a new bank account is verified:

new_account, created = BankAccount.objects.get_or_create(
user=user,
account_id=account_id,
defaults={'official_name': official_name, 'balances': balances},
)

With this setup, you’ve successfully integrated Plaid’s bank account verification into your Django backend. You can now use this verified information for secure transactions or other features in your application.

Remember to test your integration thoroughly and keep your Plaid keys secure. Happy coding!