Step-by-step
🤖 Hugging Face
Hugging Face
📘 Step-by-step 📘 Hugging Face · Hugging Face↗ Hugging Face · Official sitebeginner 💼 Business

Draft AI-powered Email Replies Using Hugging Face

Learn how to connect to Hugging Face's AI models to generate quick, polite email drafts directly from your computer, saving you valuable time on daily correspondence.

Ever wished you had a clever assistant to help you whip up email replies in a flash? This guide will show you how to use Hugging Face's Inference API to generate polite email drafts, giving you more time back in your day. By the end, you'll be able to send a simple instruction to an AI and receive a suggested response, perfect for streamlining your work email tasks and everyday correspondence.

💡 Tip: tap a step’s number when you finish it — a green tick appears and your browser remembers how far you got.

✅ Before you start
  • You'll need a free Hugging Face account. This process assumes you're using their free tier for the Inference API, which usually has usage limits and can be slower than paid versions.
  • A computer (laptop or desktop) with an internet connection.
  • Access to a terminal or command prompt on your computer. This is a text-based window where you can type instructions directly to your computer.
  • Rough total time: Allow about 15-20 minutes for your first attempt.
1

Get Your Digital Key (API Token)

First, let's get your unique digital key, known as an API token. An API (Application Programming Interface) is like a restaurant's digital menu and ordering system, allowing different computer programs to talk to each other. Your API token is like your personal, secret VIP pass that tells Hugging Face's "waiter" (the API) that you're authorised to place an order.

Open your web browser and go to the Hugging Face website. Log in to your account. Once logged in, you'll typically see a clean page, perhaps with your activity feed. Look for your profile picture (usually a circle with your initial or chosen image) in the top-right corner of the screen. Click on it. A menu will slide down or drop open, showing options like "Profile," "Settings," and "Sign Out." Select "Settings" from this menu. On the next screen, you'll see a sidebar on the left with various account settings. Click on "Access Tokens". You'll then see a button that says "New token." Click it. A small window will pop up asking for details. Give your token a descriptive name like "Email-Reply-Bot" and set the Role to "read." This "read" role means the token only allows you to receive information (like AI-generated text), not change anything on your account. Click "Generate a token." A very long, random string of characters (your API token) will appear on the screen. Immediately copy this token! You can click the copy icon next to it. Treat this token like a password – you won't see it again once you leave this page. Save it somewhere safe temporarily, like a simple text document, as you'll need it soon.

If it looks different: If your profile picture isn't in the top-right, look for a general account icon (like a person silhouette) or a menu button (often three horizontal lines, sometimes called a "hamburger menu"). The "Settings" or "Access Tokens" might be under slightly different names but will generally be found within your account management area.

You'll know it worked when you have successfully copied a long string of characters starting with hf_ (e.g., hf_ABCDEF1234567890) and saved it securely.

2

Find an AI "Chef" (Model) for Your Email

Now you need to choose an AI model, which is like selecting a specific chef for your meal. Each model is a unique computer program trained for different tasks. For drafting emails, you'll want a "text generation" model – one specifically designed to produce new text based on your instructions.

Open a new browser tab and navigate to the Hugging Face Hub. On this page, you'll see a prominent search bar, usually in the middle or near the top. In this search bar, type text-generation. As you type, a list of models might appear, or you might need to press Enter to see the results. You'll then see many different AI models. For a beginner, a good starting point is gpt2 because it's well-known and generally effective for various text tasks. You might also see distilgpt2 (a smaller, faster version) or microsoft/DialoGPT-medium. Click on the model you've chosen (e.g., click on the gpt2 model card). On the model's specific page, at the very top, you'll see its Model ID. For gpt2, the Model ID is simply gpt2. Make a note of this Model ID – you'll need it for the next step.

If it looks different: If text-generation doesn't yield good results, try looking for filters on the left side of the page and select "Text Generation" under "Tasks." The most popular models are often listed first or clearly labelled.

You'll know it worked when you have successfully noted down the Model ID (e.g., gpt2) of your chosen text generation AI model.

3

Prepare Your "Order" (API Request)

Now you'll craft the specific instruction you want to send to the AI. This instruction is called a prompt. You'll combine your API token, the model ID, and your prompt into a command using a tool called curl (pronounced "curl"). curl is a common way to send requests from your computer's terminal to web services like Hugging Face's API.

Open your computer's terminal. On Windows, you can search for "Command Prompt" or "PowerShell" in the Start menu. On Mac or Linux, search for "Terminal." Once open, you'll see a window with a blinking cursor, ready for your input. We'll use a curl command, which is a specific line of code that sends your request. Your command will include:

  • -X POST: This tells Hugging Face you're sending data (like placing an order).
  • -H "Authorization: Bearer YOUR_API_TOKEN": This includes your VIP pass (API token) to confirm you're allowed to make the request.
  • -H "Content-Type: application/json": This tells Hugging Face that you're sending information in JSON (JavaScript Object Notation) format, which is a structured way for computer programs to exchange data.
  • -d '{ "inputs": "YOUR PROMPT", "parameters": { "max_new_tokens": 50, "return_full_text": false } }': This is the actual data you're sending. "inputs" contains your prompt (the instruction for the AI). "parameters" are extra instructions, like "max_new_tokens": 50 (asking for about 50 new pieces of text, called tokens – a token is roughly four characters) and "return_full_text": false (which ensures the AI only sends back its generated reply, not your original prompt as well).
  • https://api-inference.huggingface.co/models/YOUR_MODEL_ID: This is the web address of the AI "kitchen" where your chosen model is waiting.

You need to replace YOUR_API_TOKEN with your actual token from Step 1 and YOUR_MODEL_ID with the model ID from Step 2.

If it looks different: Ensure your terminal is ready to accept commands. Some corporate networks might block curl commands. If you encounter errors like "command not found," you might need to contact your IT department or try a different computer.

You'll know it worked when you have a complete curl command string (like the example below) that includes your actual API token and chosen model ID, ready to copy and paste into your terminal.

💬 ExampleLet's say your API token is `hf_YOURUNIQUEKEY123` and your model is `gpt2`. You want to draft a polite and empathetic reply to a customer who is unhappy with a service.
curl -X POST \
  -H "Authorization: Bearer hf_YOURUNIQUEKEY123" \
  -H "Content-Type: application/json" \
  -d '{ "inputs": "The customer is unhappy with the service and wants a refund. Draft a polite and empathetic reply.", "parameters": { "max_new_tokens": 80, "return_full_text": false } }' \
  https://api-inference.huggingface.co/models/gpt2
4

Send Your Order to the AI "Kitchen"

With your complete curl command ready, it's time to send it. Carefully copy the entire command, including curl at the start and the model URL at the end. Then, go back to your open terminal window.

Paste the entire curl command into the terminal window at the blinking cursor. After pasting, press the Enter key on your keyboard. What happens next is that your computer sends this request over the internet to Hugging Face. The AI will then process your request – this process is called inference. It might take a few seconds, especially the first time you use a specific model or if Hugging Face needs to "load" the model onto its servers for you. The terminal window will usually remain blank for a short moment while it waits for a response.

If it looks different: If nothing happens for a long time (more than 20-30 seconds), or you see an immediate error like "Connection refused," double-check your internet connection and ensure your curl command is exactly as written, with no missing quotes or characters. A slow response might just mean the model is taking a moment to load.

You'll know it worked when, after a short wait, the terminal window displays new text. This text will typically start with a square bracket [ or an opening curly brace {, indicating that data has been received.

5

Read the AI's "Response" (Draft Email)

After a short wait, you'll see some text appear in your terminal. This will be in JSON (JavaScript Object Notation) format, which is a standard, organised way for programs to send data to each other. It might look a bit complex at first, but don't worry – you're looking for a specific part of it.

The JSON output will typically show a list (indicated by []) containing one or more objects (indicated by {}). Inside one of these objects, you'll find the key "generated_text". The text immediately following this key, enclosed in double quotation marks, is your AI-drafted email reply. For example, you might see something like \"generated_text\": \"Dear [Customer Name],\\n\\nThank you for reaching out to us...\" (the \n means a new line, and \\ means a single \ character). Read through this generated text. You can highlight and copy the text inside the quotation marks after "generated_text".

If it looks different: If you see error messages instead of JSON, go back and carefully check Step 3 for typos in your API token, model ID, or the structure of the JSON data (especially quotation marks). Sometimes, a very short max_new_tokens might result in a very brief or incomplete response.

You'll know it worked when you can clearly identify and copy the AI-generated email text within the \"generated_text\": section of the terminal output. You can then paste this text into your email program, customise it with names, specific details, and your brand's voice, as the AI only provides a general draft.

⚠️ Common mistakes
  • Incorrect API Token: A common mistake is a typo or missing a character when pasting your hf_ API token. Even a single incorrect character will prevent Hugging Face from authorising your request.
    • Fix: Go back to Step 1, regenerate a new token if you're unsure, and copy-paste it directly into your curl command, ensuring no extra spaces or characters are included.
  • Missing or Incorrect Headers: Forgetting to include either -H "Authorization: Bearer YOUR_API_TOKEN" or -H "Content-Type: application/json" will cause errors. These "headers" tell Hugging Face who you are and how to interpret your message.
    • Fix: Carefully compare your curl command to the example in Step 3, ensuring both -H lines are present and correctly formatted, with exact quotation marks.
  • Quotation Mark Issues in the Prompt: When copying and pasting, especially from word processors, straight double quotation marks (") can sometimes be converted into "smart quotes" (“ ”), which the terminal and JSON don't understand.
    • Fix: If you get an error related to JSON parsing or unexpected characters, manually re-type the double quotation marks around your inputs and parameters directly in the terminal or a plain text editor before pasting.
🚀 Try it now

Take a real email you need to reply to, summarise its core message into a short instruction (your prompt), and use the curl command from Step 4 with your API token and chosen model to draft a reply. See how quickly the AI provides a starting point you can then refine!

❓ Quick questions

How long does this take?

About 7 minutes — the guide has 5 steps, and you can tick each one off as you go.

Which tool do I need?

This guide uses Hugging Face Hugging Face — but the approach works very similarly in other AI assistants.

Do I need to prepare anything?
  • You'll need a free Hugging Face account. This process assumes you're using their free tier for the Inference API, which usually has usage limits and can be slower than paid versions.
  • A computer (laptop or desktop) with an internet connection.
  • Access to a terminal or command prompt on your computer. This is a text-based window where you can type instructions directly to your computer.
  • Rough total time: Allow about 15-20 minutes for your first attempt.
What mistakes should I avoid?
  • Incorrect API Token: A common mistake is a typo or missing a character when pasting your hf_ API token. Even a single incorrect character will prevent Hugging Face from authorising your request.
    • Fix: Go back to Step 1, regenerate a new token if you're unsure, and copy-paste it directly into your curl command, ensuring no extra spaces or characters are included.
  • Missing or Incorrect Headers: Forgetting to include either -H "Authorization: Bearer YOUR_API_TOKEN" or -H "Content-Type: application/json" will cause errors. These "headers" tell Hugging Face who you are and how to interpret your message.
    • Fix: Carefully compare your curl command to the example in Step 3, ensuring both -H lines are present and correctly formatted, with exact quotation marks.
  • Quotation Mark Issues in the Prompt: When copying and pasting, especially from word processors, straight double quotation marks (") can sometimes be converted into "smart quotes" (“ ”), which the terminal and JSON don't understand.
    • Fix: If you get an error related to JSON parsing or unexpected characters, manually re-type the double quotation marks around your inputs and parameters directly in the terminal or a plain text editor before pasting.

Keep reading

Was this helpful?

✦ Original step-by-step guide by AI World HQ's AI editorial team. Written in plain language, reviewed for accuracy.

← Back to all stories