Streamlining API Integration with AI: A ChatGPT-Assisted Development Journey
Introduction
I can now tell you first-hand that coding with AI is awesome.
For the following project, AI:
And I’m not alone.
92% of programmers are using AI tools - according to a recent study by Github and Wakefield Research. https://www.zdnet.com/article/github-developer-survey-finds-92-of-programmers-using-ai-tools/
And it’s leading to rapid efficiency improvements.
In a 2023 study, programmers with AI assistance completed the task 56% faster than the control group https://arxiv.org/pdf/2302.06590
McKinsey claims up to 100% efficiency improvements in their study https://www.mckinsey.com/capabilities/mckinsey-digital/our-insights/unleashing-developer-productivity-with-generative-ai
The following is an example of a challenging API integration problem I was faced with - and how I streamlined the development process with AI.
The Challenge
When a client wanted a flat file, I was floored.
We have a proprietary data asset with many real-time API integration patterns. The API is built for easy downstream integration via an optimized search API - but doesn’t have explicit functions for extracting flat files.
The client wanted a flat file for:
I had no idea how to do that, but knew I probably needed to use code.
The main challenge - I’m not a full time developer. I have a degree in computer science and can understand code, but am not up to date on my syntax or libraries.
Challenges included:
Also - API integrations present their own challenges:
And I had a tight timeframe on top of that.
The AI Solution
I explained this to my CTO, he sympathized and suggested I try coding with ChatGPT.
So that’s where I started.
I went to ChatGPT and typed in the following prompt:
ChatGPT Prompt:
write a python script that hits an api endpoint and paginates through the result set and saves to a csv file
And lo and behold - it generated some code!
But the code was flawed.
So I decided to provide ChatGPT with some clarification to rewrite the code.
First, it had no authentication.
Our API requires an API key in the header in the form of “authorization : Token your_api_key”. The generated code had no authentication.
So that was my first fix:
ChatGPT Prompt:
Our API requires an API key in the header in the form of “authorization : Token api_token_here”? - rewrite the code
And it did a pretty good job!
It was also doing the wrong pagination model.
For example, the script was looking for a variable called “has_more” - and looping pagination based on whether it was populated.
Our API just provides a variable for “total_count”.
That was my next fix.
领英推荐
ChatGPT Prompt:
Our API doesn’t have a “has_more” key and instead returns “total_count” in the “parameters” object in the JSON response - rewrite the code to paginate through the total count by page size
And again - the code looked pretty good!
It was also the wrong data model.
It was looking for the returned data in an array called “results” but ours is called “data”.
That was my next fix.
ChatGPT Prompt:
The JSON response contains an object “data” that is an array of JSON objects representing the results set - rewrite the code
Now I had my “bare bones” for iteration - and I didn’t have to “write” anything yet!
Debugging and Error Handling
I created a new Python script in VSCode, copy / pasted the AI-generated code, hit run - and immediately got an error.
This happened many more times than I’d care to admit.
Debugging can be so incredibly time intensive if you don’t have many insights and don’t know where to start.
But who better to help me debug than ChatGPT!? After all, they wrote the initial code.
For example, when I got an error from running my script, I entered the following prompt to ChatGPT:
ChatGPT Prompt:
I’m getting the following error: <error from console>
Help me debug it
And help me debug - it did!
It modified the script to include debugging mechanisms:
I could then modify the code with a few manual tweaks and ask ChatGPT for any syntax help needed along the way.
Then
Until finally I had a working product.
This saved me so much time
And the resulting product was high quality
Learnings
Conclusion
Generative AI is awesome for coding. It’s not the silver bullet that will kill your developers just yet - but it is a significant accelerator.
Benefits include:
I personally loved the experience. It’s rekindled a love for code that I haven’t felt in awhile.
For example, I hate learning and looking up syntax (ie. is it console.print or console.log or print(“text”) or printf? - I don’t give a $%^# as long as it prints!). And syntax changes drastically from language to language.
ChatGPT will write the syntax based on your description of what you want the code to do - the more descriptive the better. No more sifting through function documentation and code examples.
I love how ChatGPT helps me code!
How will you incorporate AI into your development stack today?