REFERRAL PROGRAM API DOCUMENT
1. INTRODUCTION TO API :
What is an API?
An API (Application Programming Interface) is a set of defined rules that allow different
software components to communicate with each other. It acts as a bridge between
different applications or layers, enabling data exchange and functionality without
revealing the underlying code or database structures.
Why Use an API?
• Decoupling Frontend & Backend: The client and server can be developed
independently.
• Scalability: Reusable endpoints for different platforms (web, mobile, IoT).
• Security: API abstracts the internal logic and database.
• Standardization: Follows RESTful or other standards, making development
consistent.
REFERRAL PROGRAM API :
This project is a backend API system that facilitates a referral reward mechanism for an e-
commerce or service-based platform. It allows:
• User registration (with or without a referral code)
• Generation of unique referral codes
• Credit of reward points to referrers
• Retrieval of users and their reward status
Technologies Used:
• Python
• Flask
• SQLAlchemy (ORM)
• SQLite (as the database)
CODE :
app.py:
from flask import Flask, request, jsonify
from models import db, User
from utils import generate_referral_code
from werkzeug.security import generate_password_hash
from werkzeug.exceptions import BadRequest
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db.init_app(app)
with app.app_context():
db.create_all()
@app.route('/signup', methods=['POST'])
def signup():
try:
data = request.get_json()
except BadRequest:
return jsonify({'error': 'Invalid JSON'}), 400
if not data:
return jsonify({'error': 'Missing data'}), 400
username = data.get('username')
password = data.get('password')
referral = data.get('referral_code')
if not username or not password:
return jsonify({'error': 'Username or password missing'}), 400
if User.query.filter_by(username=username).first():
return jsonify({'error': 'Username exists'}), 400
code = generate_referral_code()
while User.query.filter_by(referral_code=code).first():
code = generate_referral_code()
hashed_password = generate_password_hash(password)
new_user = User(username=username, password=hashed_password,
referral_code=code, referred_by=referral)
if referral:
referrer = User.query.filter_by(referral_code=referral).first()
if referrer:
referrer.reward_points += 10
else:
return jsonify({'error': 'Invalid referral code'}), 400
db.session.add(new_user)
db.session.commit()
return jsonify({
'message': 'User signed up successfully',
'your_referral_code': code
})
@app.route('/users', methods=['GET'])
def get_users():
users = User.query.all()
return jsonify([
{
'username': u.username,
'referral_code': u.referral_code,
'referred_by': u.referred_by,
'reward_points': u.reward_points
} for u in users
])
@app.route('/rewards/<username>', methods=['GET'])
def get_rewards(username):
user = User.query.filter_by(username=username).first()
if not user:
return jsonify({'error': 'User not found'}), 404
return jsonify({'reward_points': user.reward_points})
if __name__ == '__main__':
app.run(debug=True)
models.py :
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(128), nullable=False)
referral_code = db.Column(db.String(10), unique=True, nullable=False)
referred_by = db.Column(db.String(10), nullable=True)
reward_points = db.Column(db.Integer, default=0)
class Order(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
amount = db.Column(db.Float, nullable=False)
completed = db.Column(db.Boolean, default=False)
utils.py :
import random
import string
def generate_referral_code(length=6):
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=length))
API Endpoints and Usage :
Endpoint 1: Sign Up
• Method: POST
• URL: http://localhost:5000/signup
• Description: Registers a new user. Optionally accepts a referral code.
• Request Body (JSON):
Output :
Endpoint 2: Get All Users
• Method: GET
• URL: http://localhost:5000/users
• Description: Retrieves all users, their referral codes, who referred them, and reward
points.
• Response:
Output:
Endpoint 3: Check User Rewards
• Method: GET
• URL: /rewards/<username>
• Description: Returns total reward points for a specific user.
• Example: /rewards/swetha
• Response:
End Point 4: Create an another User By Referral Code:
Abi used the referral code of the aksaya now the rewards/aksaya :
The reward get increased by 20 points, it will also increased by purchasing the products.
6. Conclusion
This referral program API is a modular and extensible backend that can be used in e-commerce
applications to promote user growth via incentivized referrals. It demonstrates core backend
principles such as user authentication, API routing, database interaction, and referral tracking.
It can be easily extended to support email notifications, tiered rewards, or integration with
frontend apps.