Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 30, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 30, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 30, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 30, 2025

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025

      Cyberpunk 2077 sequel enters pre-production as Phantom Liberty crosses 10 million copies sold

      May 30, 2025

      EA has canceled yet another game, shuttered its developer, and started more layoffs

      May 30, 2025

      The Witcher 3: Wild Hunt reaches 60 million copies sold as work continues on The Witcher 4

      May 30, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      How Remix is shaking things up

      May 30, 2025
      Recent

      How Remix is shaking things up

      May 30, 2025

      Perficient at Kscope25: Let’s Meet in Texas!

      May 30, 2025

      Salesforce + Informatica: What It Means for Data Cloud and Our Customers

      May 30, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025
      Recent

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025

      Cyberpunk 2077 sequel enters pre-production as Phantom Liberty crosses 10 million copies sold

      May 30, 2025

      EA has canceled yet another game, shuttered its developer, and started more layoffs

      May 30, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»How to Create a DeepSeek R1 API in R with Plumber

    How to Create a DeepSeek R1 API in R with Plumber

    February 21, 2025

    To create an AI chatbot and integrate it with another platform, you have to communicate with large language model using an API. This API receives prompts from the client and sends them to the model to generate answers.

    In this tutorial, you will learn how to create such an API using the DeepSeek R1 large language model so external applications can call it. We will use the DeepSeek R1 model, available on HuggingFace, and the Plumber R package to deploy it as an API.

    HuggingFace is an open source platform for building, training, and deploying machine learning models, while Plumber is an R package that expose R code as a RESTful APIs accessible to other applications through HTTP requests.

    With this API, you can:

    • Build AI applications

    • Connect to external data and extract meaningful insights

    • Integrate into existing applications to provide customer support, create documentations, and so on.

    What is the DeepSeek R1 Model?

    DeepSeek R1 is the latest large language model from the Chinese company DeepSeek. It was designed to enhance the problem-solving and analytic capabilities of AI systems.

    DeepSeek-R1 uses reinforcement learning and supervised fine-tuning to handle complex reasoning tasks. Unlike proprietary models, DeepSeek R1 is open-source and free to use.

    Prerequisites

    • Sign up for a HuggingFace account if you don’t already have one

    • Install R and R Studio.

    • Install the plumber R package to build the API endpoint

    • Install the httr2 R package to work with HTTP requests and interact with the Hugging Face API

    Step 1: Create Your Project Repository

    You need to create an R project to create an API application in R. This ensures that all the files needed to keep your API working are kept together under the same directory. R Studio already has a template provided for API projects, so you can follow the steps below to create yours.

    In your R Studio IDE, click on the File menu and go to New Project to open the New Project Wizard. Once in the wizard, select New Directory, then click New Plumber API Project. Inside the directory name field, give it a name (for example DeepSeek-R1 API), and then click on Create Project.

    You will see a file called plumber.R with a sample API template. This is where you’ll write the code to connect to the DeepSeek R1 model on HuggingFace. Make sure that you clear this template before proceeding.

    GIF showing how to create a new Plumber project in R

    Next, go to your terminal and create a .env file. This is where you will store the Hugging Face API key.

    touch .env
    

    Image showing how to create a .env variable on the terminal

    Create a .gitignore file and add the .env file to it. This ensures that sensitive information like access tokens and API keys are not pushed to your Git repository.

    Image showing the .env file in the .gitignore file

    Step 2: Create a Hugging Face Access Token

    We need to create an access token to connect to Hugging Face models. Go to your profile, click Settings, and click Create New Token to create your access token for the Hugging Face repository.

    Image showing the access tokens page, with options to create a new token

    Copy the access token and paste it into your .env file, and give it the name HUGGINGFACE_ACCESS_TOKEN.

    HUGGINGFACE_ACCESS_TOKEN="<your-access-token>"
    

    Next is to install the dotenv package, and paste the following code at the top of your plumber.R file:

    # Load environment variables from .env
    dotenv::load_dot_env()
    

    dotenv::load_dot_env() loads all environment variables in the .env file, making them available to the plumber.R script.

    Step 3: Build the DeepSeek API Endpoint

    Now that we have our project environment set up and API token ready, we’ll write the code to build the API application by connecting to the DeepSeek R1 model on HuggingFace.

    Go to the plumber.R file and load the following libraries:

    library(plumber)
    library(httr2)
    

    Copy and paste the following code into plumber.R:

    
    api_key <- Sys.getenv("HUGGINGFACE_ACCESS_TOKEN")
    
    
    
    #* @post /deepseek_chat
    function(prompt) {
      url <- "https://huggingface.co/api/inference-proxy/together/v1/chat/completions"
    
      # Create a request object
      req <- request(url) |>
        req_auth_bearer_token(api_key) |>
        req_body_json(list(
          model = "deepseek-ai/DeepSeek-R1",
          messages = list(
            list(role = "user", content = prompt)
          ),
          max_tokens = 500,
          stream = FALSE
        ))
    
      # Perform the request and capture the response
      res <- req_perform(req)
    
      # Parse the JSON response
      parsed_data <- res |>
        resp_body_json()
    
      # Extract the content from the response
      content <- parsed_data$choices
      return(content)
    }
    

    Here’s what’s going on in the above code:

    • Sys.getenv gets the HuggingFace access token and stores it in the variable access_token.

    • The url variable contains the API link to access the DeepSeek model on HuggingFace. You can get this by searching the model name deepseek-ai/DeepSeek-R1 on HuggingFace. Go to the View Code button, and under the cURL tab, copy the API URL

      GIF showing how to copy the API url you are going to use in your plumber API code

    • #* @post /deepseek_chat means that the endpoint makes a POST request through the path /deepseek_chat.

    • This endpoint takes an argument prompt, a text, or a question a user is expected to give.

    • The req object is a chain of various operations, which makes a request() to the url, and then takes the api_key inside the req_auth_bearer_token() function. Model properties such as model name, role, prompt, and max_tokens are passed to the req object through the req_body_json function.

    • The headers variable contains the authorization required to make a request to HuggingFace API.

    • The request is performed and captured in a response object res using the req_perform() function.

    • The res object returns a JSON object, which is now parsed to R using theresp_body_json() function.

    • The content of the parsed_data is now returned so you can extract the information you need from the application for which you want to use the API.

    Step 4: Test the API Endpoint

    Let’s run the API endpoint to see how the application performs. Click on Run API. This will automatically open the API endpoint on your browser on the URL http://127.0.0.1:8634/docs/.

    Image showing the Run API button

    Click on the API endpoint dropdown, provide a prompt, and click the Execute button. You should receive a reply in a few minutes.

    Image showing how the API endpoint returns a response when a prompt is given

    Conclusion

    With your API, you can make inferences to the Hugging Face model and build AI applications in R or other programing languages. You need to host your API to make it accessible to clients online. There are various ways of hosting an R Plumber application: you can use Docker or host it on DigitalOcean using the plumberDeploy R package. However, the simplest and easiest way is to use Posit Connect.

    You can use the same approach used in this tutorial to try out other HuggingFace models, build an API to generate images or translate different languages. R Plumber is easy to use, and the documentation provides many resources.

    If you are interested in model deployment using R Plumber, you can check out this article on how to deploy a Time Series model built on Prophet using R Plumber.

    If you find this article interesting, please check my other articles on learndata.xyz.

    Source: freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleHow to Code a Crossy Road Game Clone with Three.js
    Next Article How to Build a Replit Clone with Socket.io, Monaco Editor, and Copilotkit

    Related Posts

    Security

    China-Linked Hackers Exploit SAP and SQL Server Flaws in Attacks Across Asia and Brazil

    May 30, 2025
    Security

    New Apache InLong Vulnerability (CVE-2025-27522) Exposes Systems to Remote Code Execution Risks

    May 30, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    CVE-2025-48268 – Guru Team Bot for Telegram WooCommerce Missing Authorization Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    The Dumbest Thing in Security This Week: U Did WUT?

    Development

    CVE-2025-4526 – Dígitro NGC Explorer Password Field Masking Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    No more alt-tabbing: Edge Game Assist floats guides, Discord, and more over your games

    News & Updates

    Highlights

    Artificial Intelligence

    Apple AirTag 2 Launch Is Expected In 2025: What Big Upgrades Are Coming Next Year?

    November 21, 2024

    Hey there, tech enthusiasts! If you’re a fan of Apple’s innovative products, you’re in for…

    Maximizing ROI with Experience Cloud: Best Practices for Analytics and Reporting

    January 21, 2025

    Think GeoGuessr is fun? Try using ChatGPT to guess locations in your photos

    April 18, 2025

    Essential Utilities: Reclaiming Disk Space (GUI Tools)

    April 27, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.