Michael Bissell
  • Twitter
  • LinkedIn
  • Contact me

CSV API

December 16, 2024

I made a little nodejs http server that takes a CSV file and lets you query it like a RESTful API. It doesn't support POST, PUT, PATCH or DELETE so I can only think that you would use this if you had a LOT of fairly static data that needed super high access.

Anyhow, this isn't really a blog, per se, but really just a link to the repo and my readme document from the project if you're interested.

You can check out a copy of the script on github at

https://github.com/bissellator/csvapi/tree/main

Usage

CSVAPI is a small node script that creates an API server on a CSV file. Everyone seems to have a spreadsheet, so this is just an easy way to take that data and make it querieable and sortable over a RESTful API. This can easily be run in Docker or Lambda and is handy for fairly static, but highly accessed data. It loads the entire CSV file into memory, but honestly csv files shouldn't get so huge for that to be a problem.

Once up and running you can see the entire csv file rendered as json:

GET http://localhost:8000/

[
    {
        "dogname": "Buddy",
        "breed": "Golden Retriever",
        "color": "Golden",
        "dateofbirth": "2022-01-15",
        "favoritetoy": "Tennis Ball"
    },
    {
        "dogname": "Bella",
        "breed": "Labrador Retriever",
        "color": "Black",
        "dateofbirth": "2021-09-23",
        "favoritetoy": "Squeaky Toy"
    },
    {
        "dogname": "Max",
        "breed": "German Shepherd",
        "color": "Brown",
        "dateofbirth": "2023-03-12",
        "favoritetoy": "Frisbee"
    },
    {
        "dogname": "Charlie",
        "breed": "Poodle",
        "color": "White",
        "dateofbirth": "2022-11-07",
        "favoritetoy": "Plush Squirrel"
    }
...

You can filter on individual fields with their fieldname:

GET http://localhost:8000/?favoritetoy=ball

[
    {
        "dogname": "Buddy",
        "breed": "Golden Retriever",
        "color": "Golden",
        "dateofbirth": "2022-01-15",
        "favoritetoy": "Tennis Ball"
    },
    {
        "dogname": "Oliver",
        "breed": "Poodle",
        "color": "Black",
        "dateofbirth": "2023-02-17",
        "favoritetoy": "Tennis Ball"
    },
    {
        "dogname": "Zoe",
        "breed": "Husky",
        "color": "Gray and White",
        "dateofbirth": "2022-12-25",
        "favoritetoy": "Ball"
...

If you enter a filter that is not a supported fieldname you will get an informative error:

http://localhost:8000/?kludge=ball

{
    "error": "kludge is not a valid field",
    "validkeys": [
        "dogname",
        "breed",
        "color",
        "dateofbirth",
        "favoritetoy"
    ]
}

You can also add which field to sort on and optionally include sortBy=desc to reverse sort:

http://localhost:8000/?favoritetoy=ball&sortBy=dogname&sortOrder=desc

[
    {
        "dogname": "Zoe",
        "breed": "Husky",
        "color": "Gray and White",
        "dateofbirth": "2022-12-25",
        "favoritetoy": "Ball"
    },
    {
        "dogname": "Oliver",
        "breed": "Poodle",
        "color": "Black",
        "dateofbirth": "2023-02-17",
        "favoritetoy": "Tennis Ball"
    },
    {
        "dogname": "Lily",
        "breed": "Australian Shepherd",
        "color": "Blue Merle",
        "dateofbirth": "2024-01-11",
        "favoritetoy": "Ball"
    }
...

There is no writing to the original files so, POST, PUT, PATCH and DELETE will all reaturn a 405 method not allowed message

Files

  • csvapi.js -- nodejs script that reads the csv file and presents it as a queryable restful API in JSON
  • text.csv -- sample csv file (sorry not a lot of variation in that thing)

Dependencies

This requires the following node modules:

  • http -- web host
  • fs -- reads the csv file from the filesystem
  • csv-parser -- converts the CSV file to a JSON object

ToDo

  • Add additional filters for pagination (size, start and offset)
  • Add filter for operation (eq, gt, lt and like)
⟪ Tutorial: Setting up S3 as a web host | You think you're sneaky, but we have your fingerprints ⟫


REAL RESUME FEEDBACK FROM REAL RECRUITERS
Your resume is your most valuable tool in your job search. But how do you know your resume is in top shape?

Our recruiters will review your resume line by line and give you detailed feedback on how you can improve it.

Visit mjlprojects.com to learn more!

Resume

  • My Resume
  • MJL Projects
  • NWEA Experience
  • Apigee Experience
  • Cloudentity Experience
  • Conquent Experience
  • Technical Skills
  • Presentation and Voice
  • API Standards
  • Podcasts

Articles

© Michael Bissell. All rights reserved.