Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

Pagination and Filtering with REST API’s

Filtering Methods

Filtering stands as a key functionality in REST APIs, offering users the ability to access specific data segments based on defined criteria. This precise form of data retrieval plays a crucial role in improving user experience, minimizing bandwidth consumption, and enhancing the overall efficiency of the API. By allowing clients to request exactly what they need, filtering makes the API more streamlined and user-centric.

Among the various ways to implement filtering in REST APIs, the use of query parameters is one of the most common and effective methods. These parameters are attached to the API request's URL and delineate the filtering criteria. This approach is valued for its simplicity and adaptability, making it a preferred choice in numerous API applications.

Python Flask Example

In the context of a Python Flask application, filtering can be implemented through query parameters as follows:


from flask import Flask, request, jsonify

app = Flask(__name__)

# Sample data
data = [
    {"id": 1, "name": "Alice", "age": 30},
    {"id": 2, "name": "Bob", "age": 25},
    {"id": 3, "name": "Charlie", "age": 35}
]

@app.route('/users', methods=['GET'])
def get_users():
    age = request.args.get('age')
    filtered_data = [user for user in data if user['age'] == int(age)] if age else data
    return jsonify(filtered_data)

if __name__ == '__main__':
    app.run(debug=True)

Expected Output:

Requesting /users?age=30 will return:
[
    {"id": 1, "name": "Alice", "age": 30}
]

Node.js Example

Similarly, in a Node.js server using Express:


const express = require('express');
const app = express();

// Sample data
const data = [
    { id: 1, name: 'Alice', age: 30 },
    { id: 2, name: 'Bob', age: 25 },
    { id: 3, name: 'Charlie', age: 35 }
];

app.get('/users', (req, res) => {
    const age = req.query.age;
    const filteredData = age ? data.filter(user => user.age === Number(age)) : data;
    res.json(filteredData);
});

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});


Expected Output:

Requesting http://localhost:3000/users?age=30 will return:

[
    { "id": 1, "name": "Alice", "age": 30 }
]

In both Python and Node.js scenarios, these examples demonstrate how query parameters can be utilized for filtering. By incorporating an age parameter, the API zeroes in on users who match the specified age. This technique stands out for its efficiency, ease of implementation, and broad applicability across a variety of API types.

Implementing Pagination and Filtering

In the world of REST APIs, managing and presenting large amounts of data effectively is a key concern. This is precisely where pagination and filtering come into play. These functionalities not only make data retrieval more efficient but also greatly enhance the user's experience. Let's explore how to implement pagination and filtering in Python using Flask and in Node.js, giving insight into the workings of the code and what to expect from it.

Python Flask Pagination Example

Implementing pagination in a Flask application involves dealing with page and limit parameters in the API request. The page parameter specifies the current page number, and limit determines the number of records on each page. This method facilitates smooth navigation and handling of data, especially in cases with large datasets.

Here's a look at how pagination can be implemented in a Flask application:

from flask import Flask, request, jsonify

app = Flask(__name__)

# Sample data: Simulating a list of users
users = [
    {"id": i, "name": f"User{i}", "age": 20 + i} for i in range(1, 101)
]

@app.route('/users', methods=['GET'])
def get_users():
    page = request.args.get('page', 1, type=int)
    limit = request.args.get('limit', 10, type=int)
    start = (page - 1) * limit
    end = start + limit
    return jsonify(users[start:end])

if __name__ == '__main__':
    app.run(debug=True)

Expected Output:

When /users?page=2&limit=10 is requested, it retrieves the second set of 10 users, effectively demonstrating how pagination divides the data into accessible segments.

Node.js Pagination Example

In a Node.js setup using Express, pagination works by segmenting the dataset according to the page and limit parameters provided. This technique is key in breaking down large datasets into smaller parts, reducing server load and enhancing response speed.


const express = require('express');
const app = express();

// Sample data: A range of user objects
const users = Array.from({ length: 100 }, (_, i) => ({
    id: i + 1,
    name: `User${i + 1}`,
    age: 20 + i
}));

app.get('/users', (req, res) => {
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    const start = (page - 1) * limit;
    const end = start + limit;
    res.json(users.slice(start, end));
});

app.listen(3000, () => {
    console.log('Server running on port 3000');
});

Expected Output:

A request to http://localhost:3000/users?page=2&limit=10 will fetch users 11 to 20, illustrating the pagination process in a Node.js environment. Filtering Example in Flask Building on the Flask example, we introduce a filtering function. This addition enables users to retrieve data that aligns with specific criteria, such as age, further improving the API's functionality.


@app.route('/users', methods=['GET'])
def get_users():
    # Pagination parameters
    page = request.args.get('page', 1, type=int)
    limit = request.args.get('limit', 10, type=int)
    start = (page - 1) * limit
    end = start + limit

    # Filtering parameter: age
    age = request.args.get('age')
    filtered_users = [user for user in users if user['age'] == int(age)] if age else users
    return jsonify(filtered_users[start:end])

Expected Output:

A request to /users?page=1&limit=10&age=25 will filter and return the first 10 users who are 25 years old.

Filtering Example in Node.js

In Node.js, combining pagination with filtering provides the capability to query specific data subsets, making the API more adaptable and user-centric.

app.get('/users', (req, res) => {
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    const age = parseInt(req.query.age);

    let filteredUsers = age ? users.filter(user => user.age === age) : users;
    const start = (page - 1) * limit;
    const end = start + limit;
    res.json(filteredUsers.slice(start, end));
});

Expected Output:

Requesting http://localhost:3000/users?page=1&limit=10&age=25 yields the first group of users aged 25, showcasing the integration of filtering with pagination.

These examples demonstrate how pagination and filtering can be implemented in REST APIs using Python and Node.js. For developers aiming to build scalable, efficient, and user-friendly APIs, understanding and applying these concepts is vital. Efficiently navigating through large datasets and retrieving specific data points are essential skills in modern web development, and these examples offer a practical foundation for such implementations.