Attachment summarization using .NET, OpenAI API Assistant and Twilio SendGrid
Time to read:
Attachment summarization using .NET, OpenAI API Assistant and Twilio SendGrid
Introduction
Processing files is essential in many business domains. One of the common needs is summarization of text, and Generative AI has made this an easy and effortless task. In this post, you’ll learn how to build a .NET API that receives attachments and uses OpenAI API assistance to summarize files. You'll then receive an email notification when the summarization is complete using Twilio Sendgrid.
Prerequisites
- A free Twilio SendGrid account
- Visual Studio Code
- .NET 8 SDK
- OpenAI platform account Note that the OpenAI account requires an initial $5 credit balance.
The first thing you will need to do is to configure the summarization process.
Setting up OpenAI API Assistant
You will use OpenAI API Assistant, which will be configured to process the text files and summarize them.
Login or signup to the assistant’s page. Then click on the Create button, which will open a form to start building the assistant.


Fill in assistant details as follows:
- Name: “File Summarizer”
- Instructions: “You are an assistant that reads and summarizes the content of uploaded text files. Your job is to extract the main points clearly and concisely for users without losing important context”
- Model: gpt-4-turbo
Then configure the tools to use File Search to be able to read and summarize the files properly. Set the temperature to a value less than 1 to decrease the amount of randomness and avoid AI hallucinations.
After you are done configuring the assistant, copy and keep its ID. You will need it in the implementation of the application in .NET.


Before you start building the application, you can test the assistant first in the assistant’s playground by clicking on the Playground button as shown below:


It will then open the playground as shown below. You will be able to attach and summarize your files by clicking the attached icon and choosing File Search. Attach your document. The summarizer supports the filetypes .txt, .pdf or .docx – you can check if the file you have is supported in the OpenAI Supported Files List. Then run the assistant.


Then it will generate the summary for your text as shown below:


You can tweak the summarization instructions or the prompt itself when you attach the file to make it more customized, or provide examples of how the output needs to look. When you reach results that you are satisfied with, then move on to using the assistant’s API in your application.
Before you start using the API you need to generate an API Key. To create the API Key, navigate to API Keys page here, then click on Create New Secret Key.
Add a name to your secret key, then click Create Secret Key. It will then show your generated API key. It is important to copy it and save it in secure storage because you will not be able to see it again for security standards.
Set up SendGrid API Key
You’ll need to create a new SendGrid API Key with restricted access. On the Restricted Access tab, be sure your API Key is set to allow Email Sending access. Give your API key a name, and click Create + View. Keep this key somewhere safe as you will need it in the subsequent steps, and SendGrid will not show it again.
Building the .NET API Application
Open Visual Studio Code and create a new, empty folder. Open the terminal window and run the following commands to create the .NET web API:
Run the following commands to install the needed packages for Sendgrid, JSON and HTTP Features:
Open the file AppSettings.json. Beneath the existing logging settings and between the outer brackets, add the following code to configure OpenAI and SendGrid.
Replace the OpenAI API key value with the one you obtained in the previous steps, and add the ID of the assistant you created in OpenAI for summarization. Replace the API key for SendGrid with the one you obtained for your account and the “FromEmail” with the email you authorized to use.
Create a Controllers folder in the AttachmentSummarizer directory. Then, create a new file named UploadController.cs. Add the following code to create the endpoint to upload an attachment and receive the attachment’s summary by email:
Some of this code will not be working yet. However, we'll add the necessary services for your application in the next step.
Implement OpenAi assistant API logic
Create a Services folder. Then create a new file called OpenAIService.cs and add the following code to implement the assistant call then send the attachment, and run the summarization request:
The code above performs the following steps to send and summarize the file:
- Upload File to OpenAI
- Uploads the user-provided file using
purpose=assistants.
- Returns a
file_id.
- Uploads the user-provided file using
- Create Vector Store
- Creates a vector store and attaches the uploaded file to it.
- Returns a
vector_store_id.
- Wait for Vector Store to Be Ready
- Polls the vector store’s status until it is completed (fully indexed and ready for search).
- Create a New Thread
- Initializes a conversation thread where messages and assistant runs are tracked.
- Returns a
thread_id.
- Add a User Message
- Adds a message to the thread with instructions like “Please summarize the uploaded document.”
- Ensures the assistant has context for what to do.
- Create Assistant Run with File Search
- Starts the assistant run, providing both the
assistant_id
andvector_store_id
intool_resources.file_search
. - Enables the assistant to access and search the uploaded file.
- Starts the assistant run, providing both the
- Poll Run Until Completed
- Periodically checks the run’s status (
queued, in_progress,
etc.) until it reachescompleted
.
- Periodically checks the run’s status (
- Retrieve Assistant’s Response
- Fetches the final summary message from the thread.
- Returns the assistant’s response as a string.
Implement SendGrid email service
Create a new file, EmailService.cs, in your Services folder. Add the following code to implement the email send logic:
Register the services
Open the Program.cs file, and replace the entire code with the following code, adding the services created for OpenAI and SendGrid:
Testing the application
Run the following command to run the application:
Copy the URL of your localhost running endpoint and append to it “/api/upload/summarize” to call the endpoint you are testing. Open Postman and use a POST request with the body configured as “form-data” with the first item name “file” of type File then attach the file you want to summarize. Then add another item with the name “email” and add the email you want to receive the summary on.


You should then receive a summary to your email that will look like the sample below:


Troubleshooting your application
If you receive any errors or you don’t receive the email, make sure you check the following:
- Make sure that your sender email is added as a verified email in SendGrid.
- Check your spam folder
- Make sure the receiver email is correct
- Make sure you have valid API keys for both SendGrid and OpenAI
Wrapping up
Summarization is one of the features LLMs excel at, enabling users to quickly extract meaningful insights from large volumes of text with minimal effort. By integrating OpenAI’s Assistants API into your .NET application, you can automate this process across various file formats, delivering concise, context-aware summaries directly to users. Combined with tools like Twilio SendGrid, the solution becomes not just intelligent, but also actionable, empowering users with relevant information right when they need it.
Eman Hassan, a dedicated software engineer and a technical author, prioritizes innovation, cutting-edge technologies, leadership, and mentorship. She finds joy in both learning and sharing expertise with the community. Explore more courses, code labs and tutorials authored by Eman here https://app.pluralsight.com/profile/author/eman-m
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.