Blog/
Building a RAG Assistant for API Security
Learn how I built a RAG-powered assistant that pulls API security answers from OWASP and NestJS docs, with real citations and a refusal fallback.

Why RAG Matters for API Security
RAG, or Retrieval Augmented Generation, lets a language model pull up-to-date facts from external documents instead of relying on its internal knowledge base. For developers working on API security, the most reliable guidance comes from official docs and industry standards like the OWASP API Security Top 10. A RAG assistant can surface that information on demand, keeping the answers accurate and traceable.
I spent a weekend digging into RAG beyond the usual “chat with a PDF” wrappers. The goal was to build a small, practical assistant that answers questions about API security using two trusted sources: the OWASP API Security Top 10 (2023) and the NestJS security docs. The assistant can answer queries such as “How do I enable CORS in NestJS?” by retrieving relevant chunks, generating a concise answer, and displaying citations that the user can verify.
Designing the Assistant
The core idea is simple: store the documentation in a vector database, retrieve the most relevant chunks for a user query, and let a generation model compose an answer. The assistant is built with a few key components:
- Chunking , Splits the docs into meaningful pieces while preserving headings and code blocks.
- Embeddings , Converts each chunk into a vector using Jina, then stores it in Supabase with the pgvector extension.
- Retrieval , Uses vector similarity search to pull the top-k chunks.
- Generation , Feeds the retrieved chunks and the user question to Groq’s LLM, which writes the answer.
- Refusal logic , If the model can’t find enough context, it refuses instead of hallucinating.
Chunking the Docs
A naive split can break code blocks or merge unrelated sections, leading to confusing answers. I kept the original headings and code blocks intact by using a custom chunker that respects Markdown syntax. Each chunk is small enough for efficient embedding but large enough to provide context. This approach keeps the answer coherent and the citations precise.
Storing Embeddings in Supabase
Supabase offers a Postgres database with the pgvector extension, making it a lightweight yet powerful vector store. After generating embeddings with Jina, I inserted them into a table with columns for the chunk text, source, and vector. The vector column allows fast similarity queries, which are critical for real-time retrieval.
Retrieval and Evaluation
Before building the UI, I ran a quick evaluation to gauge the retrieval quality. Using 14 hand-written questions, the baseline vector search achieved a Hit@5 of 0.93 and a Mean Reciprocal Rank of 0.80. These numbers are good enough for a demo but leave room for improvement. The next steps include hybrid search, reranking, and expanding the evaluation set.
Handling Uncertainty with Refusal
A common pitfall in RAG systems is the model confidently answering a question it has no evidence for. To avoid this, I added a prompt that explicitly allows the model to refuse if the retrieved context does not cover the query. This behavior is preferable to a hallucinated answer that looks like documentation but is actually made up.
Building the Frontend with Next.js
The user interface is a lightweight Next.js app. It accepts a question, sends it to the backend, and displays the generated answer along with clickable citations that link back to the original source. The UI is intentionally minimal to keep the focus on the RAG logic.
Deployment and Maintenance
Because the free tier of Supabase can go to sleep, I added a keep-alive cron job that pings the database periodically. The stack consists of:
- Next.js for the frontend
- Supabase + pgvector for storage
- Jina for embeddings
- Groq for generation
The entire project is open-source and available on GitHub: https://github.com/abdulwahb1/rag-security-app.
Next Steps and Future Work
The current prototype demonstrates that a RAG assistant can provide accurate, citation-rich answers for API security questions. Future enhancements will focus on:
- Hybrid search that combines keyword matching with vector similarity
- Reranking the retrieved chunks to improve relevance
- A larger, automated evaluation set to track progress
- Adding more security documentation sources
Wrap Up
RAG is a powerful tool for developers who need reliable, up-to-date information from official docs. By combining chunking, embeddings, and a refusal-aware prompt, I built a small assistant that answers API security questions with real citations. If you’re curious about building similar tools or want to discuss freelance or full-stack work, feel free to reach out at wabby.dev.
Written by Abdul Wahab
← All posts