<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Andrew Rowley</title>
    <description>The latest articles on DEV Community by Andrew Rowley (@internetdrew).</description>
    <link>https://dev.to/internetdrew</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F928997%2Fca0ee2f7-c8e9-4e97-8c78-9e38a9626306.jpeg</url>
      <title>DEV Community: Andrew Rowley</title>
      <link>https://dev.to/internetdrew</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/internetdrew"/>
    <language>en</language>
    <item>
      <title>Sending Files to Cloudinary Over REST API with Next.js</title>
      <dc:creator>Andrew Rowley</dc:creator>
      <pubDate>Tue, 26 Sep 2023 19:08:47 +0000</pubDate>
      <link>https://dev.to/internetdrew/sending-files-to-cloudinary-over-rest-api-with-nextjs-g63</link>
      <guid>https://dev.to/internetdrew/sending-files-to-cloudinary-over-rest-api-with-nextjs-g63</guid>
      <description>&lt;p&gt;If you're struggling to take a file from user input and get it to your API so you can upload it to Cloudinary and send your &lt;code&gt;secure_url&lt;/code&gt; back over to the front end of your application, this is for you.&lt;/p&gt;

&lt;p&gt;When I tried figuring this out myself, there were lots of complicated videos and incomplete tutorials that made it a bit difficult to understand. But with this approach, you can easily send your files from your file input to your rest API, send that file up to Cloudinary, and return your &lt;code&gt;secure_url&lt;/code&gt; for later usage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why This Matters
&lt;/h2&gt;

&lt;p&gt;You've probably seen documentation and tutorials using Cloudinary directly in the client, either with an SDK or directly to the API. The problem with doing so is that every time you run the operation of sending the file over to Cloudinary, your endpoint is visible in the Network tab of your browser, making the endpoint vulnerable to malicious behavior. By handling this as a restful API, we can do all of the work on the server and hide any secure URLs, only exposing our endpoint in the browser, which will be &lt;code&gt;/api/upload&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Side note&lt;/strong&gt;: This article largely addresses getting access to the file itself on the server side. So if you are not using Cloudinary, you can still follow along and deviate where I start diving into the Cloudinary SDK.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating and Managing Our Form Data
&lt;/h2&gt;

&lt;p&gt;I will also be a bit opinionated here because of how difficult form data can be in React. For everything I bring in, I'll explain how it saves you headaches.&lt;/p&gt;

&lt;p&gt;First, we start with our &lt;code&gt;Form&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function Form() {
  return (
    &amp;lt;main&amp;gt;
      &amp;lt;form&amp;gt;
        &amp;lt;input type='file' /&amp;gt;
        &amp;lt;button type='submit'&amp;gt;Send&amp;lt;/button&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/main&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first thing I will bring in to easily manage the form data is &lt;a href="https://react-hook-form.com/" rel="noopener noreferrer"&gt;React Hook Form&lt;/a&gt;. Managing this one input doesn't warrant this package, but since most forms have multiple inputs of varied types, I strongly suggest you install this to make managing your data simple.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-hook-form
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have this installed, you can bring a few things in we will need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useForm } from 'react-hook-form';

export default function Form() {
const {
    register,
    handleSubmit,
  } = useForm();

  return (
    &amp;lt;main&amp;gt;
      &amp;lt;form&amp;gt;
        &amp;lt;input type='file' /&amp;gt;
        &amp;lt;button type='submit'&amp;gt;Send&amp;lt;/button&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/main&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll use &lt;code&gt;register&lt;/code&gt; and &lt;code&gt;handleSubmit&lt;/code&gt; to take a closer look at our data. We use &lt;code&gt;register&lt;/code&gt; to register our input data. In this instance, we'll register our file input as 'file'. This will be attached to the data object we get upon submission. To see that data object, we also need to use &lt;code&gt;handleSubmit&lt;/code&gt;, which handles a submit function, which we can make here as &lt;code&gt;onSubmit&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useForm } from 'react-hook-form';

const onSubmit = async (data) =&amp;gt; {
  const { file } = data;
  console.log(file);
};

export default function Form() {
const {
    register,
    handleSubmit,
  } = useForm();

  return (
    &amp;lt;main&amp;gt;
      &amp;lt;form onSubmit={handleSubmit(onSubmit)}&amp;gt;
        &amp;lt;input type='file' {...register('file')} /&amp;gt;
        &amp;lt;button type='submit'&amp;gt;Send&amp;lt;/button&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/main&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we submit, we get our data object, as seen in &lt;code&gt;onSubmit&lt;/code&gt;. From there, we can de-structure the file. If we had another input and registered it as 'name', for instance, we could also de-structure the {name} from it, as the data object would have both &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;file&lt;/code&gt; properties because of &lt;code&gt;register&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FFileList {0: File, length: 1}
 0: File
      lastModified: 1684160876984
      lastModifiedDate: Mon May 15 2023 10:27:56 GMT-0400 (Eastern Daylight Time) {}
      name: "filename.pdf"
      size: 69759
      type: "application/pdf"
      webkitRelativePath: ""
       [[Prototype]]: File
      length: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What you'll see when you &lt;code&gt;console.log(file)&lt;/code&gt; is a &lt;code&gt;FileList&lt;/code&gt;. At index &lt;code&gt;0&lt;/code&gt; will be our file.&lt;/p&gt;

&lt;p&gt;Now that we have our file, we can push it over to our API where we want to safely post to Cloudinary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You could use the Fetch API for this, but I found there were issues with headers when using fetch. So... use Axios and save headaches on this one (or figure it out by digging around, if you truly want fetch).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const onSubmit = async (data) =&amp;gt; {
  const { file } = data;

  const res = await axios.post('/api/upload', file);
  console.log(res)
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With a &lt;code&gt;POST&lt;/code&gt; method, we push our file over to the other side, which is most likely where you started wanting to rip your hair out in confusion if you've attempted this before.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing the File Object on the Server
&lt;/h2&gt;

&lt;p&gt;Depending on how you started your Next app, you'll either want to go to &lt;code&gt;/src/pages/api&lt;/code&gt; or &lt;code&gt;/pages/api&lt;/code&gt;. Next.js API Routes allow you to build your own API using Next.js, so there's no need to bring in Express (unless you want to). You can &lt;a href="https://nextjs.org/docs/pages/building-your-application/routing/api-routes" rel="noopener noreferrer"&gt;learn more about Next.js API Routes here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Within the API directory, you can see a &lt;code&gt;hello.js&lt;/code&gt; file. You can take a look at that to see how things work and change the endpoint, but it's pretty straightforward, so also feel free to just rename that or create a new &lt;code&gt;upload.js&lt;/code&gt; file. That's what enables us to send the &lt;code&gt;POST&lt;/code&gt; request over to &lt;code&gt;/api/upload&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;upload.js&lt;/code&gt;, we need to export the handler function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default async function handler(req, res) {
  console.log(req.body);
  return res.status(200).json('you made it.');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you look at the request body, you'll see the string &lt;code&gt;'[object FileList]'&lt;/code&gt;. But you don't need a string. You need a file! To get that file, we're going to use &lt;a href="https://www.npmjs.com/package/formidable" rel="noopener noreferrer"&gt;Formidable&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install formidable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use Formidable, we'll use a &lt;code&gt;Promise&lt;/code&gt; as an efficient way to actually get the &lt;code&gt;file&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since we want Formidable to do all of the handling of the request, we'll want to stop automatic body parsing or we won't be able to access the file. So be sure to include the config export in the server so it turns off automatic request body parsing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import formidable from 'formidable';

export const config = {
  api: {
    bodyParser: false,
  },
};

export default async function handler(req, res) {
  const file = await new Promise((resolve, reject) =&amp;gt; {
    const form = formidable();

    form.parse(req, (err, fields, files) =&amp;gt; {
      if (err) return reject(err);
    });
    form.on('file', (formName, file) =&amp;gt; {
      resolve(file);
    });
  });

  console.log(file);
  return res.status(200).json('you have the file.');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what's happening above. First, we make sure that our handler is async. Within there, we define the &lt;code&gt;file&lt;/code&gt; variable for when we resolve/reject the promise. We create an instance of formidable (&lt;code&gt;form&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The first thing Formidable will do is parse the request, and with a callback function that takes in &lt;code&gt;error&lt;/code&gt;, &lt;code&gt;fields&lt;/code&gt;, and &lt;code&gt;files&lt;/code&gt;, we can do some management and gain visibility. &lt;/p&gt;

&lt;p&gt;We don't actually get the file from here, but we can see if a file has made it successfully and is being detected if we &lt;code&gt;console.log(files)&lt;/code&gt;. Since we don't access files directly from there, we just return the rejection of the promise along with the error if one occurs.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;form.on&lt;/code&gt;, however, we can tell formidable what we want to do when it detects a file. Here, we're telling formidable that when it does detect a file, we want it to resolve the promise with that file. We now have access to the &lt;code&gt;file&lt;/code&gt; object we sent over from the front end! You should now see it logged as a &lt;code&gt;PersistentFile&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Uploading Your File to Cloudinary
&lt;/h2&gt;

&lt;p&gt;While you can use the API endpoint directly, I used the SDK for a little simplicity (and easy clues on how to use it).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i cloudinary
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in &lt;code&gt;/api/upload&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { v2 as cloudinary } from 'cloudinary';

cloudinary.config({
  cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
  secure: true,
  api_key: process.env.CLOUDINARY_API_KEY,
  api_secret: process.env.CLOUDINARY_API_SECRET,
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your Next app, you should see a file named &lt;code&gt;.env.local&lt;/code&gt;. If you don't see it, feel free to create it. &lt;/p&gt;

&lt;p&gt;This is a safe place to store your environment variables. You can store sensitive strings here. To keep this file from being added to your git repo on commit, be sure to add this to your &lt;code&gt;.gitignore&lt;/code&gt; file, so your sensitive data will be safe. &lt;/p&gt;

&lt;p&gt;To access these values on the server, you just add process.env.WHATEVER_YOU_NAMED_IT server side. You can &lt;a href="https://nextjs.org/docs/app/building-your-application/configuring/environment-variables" rel="noopener noreferrer"&gt;learn more about environment variables in Next.js here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Before you can push the files to Cloudinary, you may need to change some settings. For example, in my use case, there is a strong likelihood that files will be PDFs, which Cloudinary does not allow you to upload by default. You might see that one page that says in order to unlock this capability you need to contact them, but that might just be old collateral. You can actually just &lt;a href="https://cloudinary.com/documentation/upload_images#unsigned_upload" rel="noopener noreferrer"&gt;make some changes in your settings here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, with access to our &lt;code&gt;file&lt;/code&gt; object, we can call on Cloudinary's uploader for &lt;code&gt;unsigned_upload&lt;/code&gt; (again, you might not need to change settings depending on what you're doing). The SDK then expects two things. One is the 'file', which has the type of &lt;code&gt;String&lt;/code&gt;, so what you really need is the &lt;code&gt;filepath&lt;/code&gt; from within the file object. The other is the name of the preset from the settings I mentioned before.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try {
    const data = await cloudinary.uploader.unsigned_upload(
      file.filepath,
      'preset-name'
    );
    return res.status(200).json(data.secure_url);
  } catch (error) {
    console.error(error);
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you do that, you can come back to the front end where your API call will now be met with the response from Cloudinary. I made the API call to just get back the &lt;code&gt;secure_url&lt;/code&gt; for the uploaded file, which is why I sent back &lt;code&gt;data.secure_url&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And that's it. A simple way to access your files with a RESTful API in Next.js!&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>tutorial</category>
      <category>javascript</category>
      <category>api</category>
    </item>
  </channel>
</rss>
