Dev.to API 002 - Creating a Post
Tags: todayilearned, showdev, api, integrations
Published: 2020-04-21 23:43:11

<h2>
<a id="user-content-002---creating-a-post-with-the-api" class="anchor" href="#002---creating-a-post-with-the-api" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>002 - Creating a Post with the API</h2>
<p>Ok, now we already know what to do to get the feed and the basic to work with the API, so, now we're going to post something using the API. To this, we must send a <code>POST</code> request to <code>/articles</code> with a the information about the post, see all the parameters possibilities at the <a href="https://docs.dev.to/api/" rel="nofollow">api documentation</a>.</p>
<h4>
<a id="user-content-curl" class="anchor" href="#curl" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>curl</h4>
<div class="highlight highlight-source-shell"><pre>curl --request POST  
--url https://dev.to/api/articles  
--header <span class="pl-s"><span class="pl-pds">'</span>api-key: YOUR_API_TOKEN<span class="pl-pds">'</span></span>  
--header <span class="pl-s"><span class="pl-pds">'</span>content-type: application/json<span class="pl-pds">'</span></span>  
--data <span class="pl-s"><span class="pl-pds">'</span>{ </span>
<span class="pl-s"> "article": { </span>
<span class="pl-s">     "title": "Creating new post", </span>
<span class="pl-s">     "published": true, </span>
<span class="pl-s">     "body_markdown": "Hey there, here go some post.", </span>
<span class="pl-s">     "tags": [ </span>
<span class="pl-s">         "todayilearned", </span>
<span class="pl-s">         "showdev" </span>
<span class="pl-s">     ]</span>
<span class="pl-s"> }</span>
<span class="pl-s">}<span class="pl-pds">'</span></span></pre></div>
<h4>
<a id="user-content-node" class="anchor" href="#node" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>node</h4>
<div class="highlight highlight-source-js"><pre><span class="pl-k">var</span> unirest <span class="pl-k">=</span> <span class="pl-c1">require</span>(<span class="pl-s"><span class="pl-pds">"</span>unirest<span class="pl-pds">"</span></span>);

<span class="pl-smi">unirest</span>.<span class="pl-en">post</span>(<span class="pl-s"><span class="pl-pds">'</span>https://dev.to/api/articles<span class="pl-pds">'</span></span>)
 .<span class="pl-c1">type</span>(<span class="pl-s"><span class="pl-pds">'</span>json<span class="pl-pds">'</span></span>)
 .<span class="pl-c1">headers</span>({
   <span class="pl-s"><span class="pl-pds">'</span>api-key<span class="pl-pds">'</span></span><span class="pl-k">:</span><span class="pl-s"><span class="pl-pds">'</span>YOUR_API_TOKEN<span class="pl-pds">'</span></span>
 })
 .<span class="pl-c1">send</span>({
   <span class="pl-s"><span class="pl-pds">"</span>article<span class="pl-pds">"</span></span><span class="pl-k">:</span> {
     <span class="pl-s"><span class="pl-pds">"</span>title<span class="pl-pds">"</span></span><span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">"</span>Creating new post<span class="pl-pds">"</span></span>,
     <span class="pl-s"><span class="pl-pds">"</span>published<span class="pl-pds">"</span></span><span class="pl-k">:</span> <span class="pl-c1">true</span>,
     <span class="pl-s"><span class="pl-pds">"</span>body_markdown<span class="pl-pds">"</span></span><span class="pl-k">:</span> <span class="pl-s"><span class="pl-pds">"</span>Hey there, here go some post.<span class="pl-pds">"</span></span>,
     <span class="pl-s"><span class="pl-pds">"</span>tags<span class="pl-pds">"</span></span><span class="pl-k">:</span> [
       <span class="pl-s"><span class="pl-pds">"</span>todayilearned<span class="pl-pds">"</span></span>,
       <span class="pl-s"><span class="pl-pds">"</span>showdev<span class="pl-pds">"</span></span>
     ]
   }
 })
 .<span class="pl-c1">then</span>((<span class="pl-smi">response</span>)<span class="pl-k">=&gt;</span>{
   <span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-smi">response</span>.<span class="pl-c1">body</span>)
 })</pre></div>
<p>That's all folks!! You can see all the steps on my <a href="https://rodcordeiro.github.io/TodayILearned/Dev.to_API" target="_blank">documentation</a>.</p>