snipt

Ctrl+h for KB shortcuts
#39520

Markdown

Snipt API

The Snipt API provides all of the functionality needed to interact with public and private snipts. The entire Snipt UI is actually powered by the API behind the scenes. If you have an issue with the API, you have a few options:

  • Ask "nicksergeant" in #snipt on irc.freenode.net.
  • Use the comments attached to this snipt.
  • Reply to us on Twitter.
  • File a support ticket on GitHub

Overview

The API is made up of a collection of resources. These resources are separated by whether they are public or private resources. There are two main API endpoints:

https://snipt.net/api/public/

https://snipt.net/api/private/

These endpoints return data in the format based on an HTTP header that you send along in the request. For example, if you would like XML returned:

Accept: application/xml

The following content types are supported: json, jsonp, xml, yaml, plist.

To preview an API response in your browser without sending along a custom HTTP request header, you can append the requested content type in the query string, like so:

https://snipt.net/api/public/?format=json


Public API

The public API is simply that: public. It's restricted to GET requests, only. You make requests, get data back, do something with that data, etc.

When you get snipts from the public API, you only get snipts back that have been marked as public. Same with tag counts - those counts will only reflect snipts that've been tagged and are also public.


Private API

The private API allows you to write apps that interact with a user's snipts. It supports almost everything that the Snipt website supports, as the website itself uses the API as its backend.

Authentication

To authenticate with the private API, you have two options. The first option is to pass an Authorization header along with your request, like this:

Authorization: ApiKey username:api_key

We've created a test user for testing the private API:

Username: api_test_user

API key: a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb

The second option for authenticating with the private API is to pass along the username and API key in the query string, like this:

https://snipt.net/api/private/?username=api_test_user&api_key=a0eb...


Resources

Snipt

Get public snipts:

GET https://snipt.net/api/public/snipt/
  • Optional parameters: limit, offset, order_by, tag, user, q
  • If provided, order_by can be one of:
    • created (oldest first)
    • -created (newest first)
    • modified (oldest first)
    • -modified (newest first)
  • If provided, user must be the ID (integer) of a user, which you can retrieve via a Snipt or User resource (see examples below). Providing a user flag will only return snipts created by that user.
  • If provided, tag must be the ID (integer) of a tag, which you can retrieve via a Snipt or Tag resource (see examples below). Providing a tag flag will only return snipts tagged with that tag.
  • If provided, q is a search term to filter results by.

Get public snipts tagged with php:

GET https://snipt.net/api/public/snipt/?tag=9
  • Note: you can get the ID of a tag from the Tag resource below.

Get public snipts ordered by oldest-first:

GET https://snipt.net/api/public/snipt/?order_by=created

Search for public snipts that match flappers:

GET https://snipt.net/api/public/snipt/?q=flappers

Get public snipts created by a specific user:

GET https://snipt.net/api/public/snipt/?user=3
  • You can get the ID of a user from the User resource or within a Snipt resource.

Get an individual snipt:

GET https://snipt.net/api/public/snipt/39388/?format=json

Get a user's snipts from the private API:

GET https://snipt.net/api/private/snipt/
    Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb
  • Optional parameters: limit, offset, order_by, tag, q
  • If provided, order_by can be one of:
    • created (oldest first)
    • -created (newest first)
    • modified (oldest first)
    • -modified (newest first)
  • If provided, tag must be the ID (integer) of a tag, which you can retrieve via a Snipt or Tag resource (see examples below). Providing a tag flag will only return snipts tagged with that tag.
  • If provided, q is a search term to filter results by.

Get a user's individual snipt from the private API:

GET https://snipt.net/api/private/snipt/39597/
    Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb

Create a snipt:

POST https://snipt.net/api/private/snipt/
     Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb
     Content-Type  application/json
     {"title": "A snipt", "lexer": "text"}
  • Required fields: lexer
  • Optional fields: title, code, tags, public, blog_post
  • lexer must be one of these (the short / lowercase code): https://snipt.net/api/public/lexer/
  • If provided, tags is a string of tags, like this: tag-1, tag2, "tag 3", etc.

Edit a snipt (partial):

PATCH https://snipt.net/api/private/snipt/39598/
      Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb
      Content-Type  application/json
      {"title": "A snipt"}
  • Note: you can also use PUT, but you'll need to send along the entire snipt object, or omitted fields will be erased.

Delete a snipt:

DELETE https://snipt.net/api/private/snipt/39595/
       Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb

Lexer

Get all lexers:

GET https://snipt.net/api/public/lexer/

User

Get public information on a specific user:

GET https://snipt.net/api/public/user/3/

If you don't know the ID:

GET https://snipt.net/api/public/user/?username=nick

Get private information on a specific user from the private API:

GET https://snipt.net/api/private/user/9971/
    Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb

Tag

Get public tags:

GET https://snipt.net/api/public/tag/
  • This will only retrieve tags that have been assigned to a public snipt.
  • If provided, q is a search term to filter results by. Give it the tag slug, such as: https://snipt.net/api/public/tag/?q=php

Get a user's tags from the private API:

GET https://snipt.net/api/private/tag/
    Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb
  • This will return all tags that have been used by this user.

Favorite

Favorites are only available via the private API.


Get a user's favorites from the private API:

GET https://snipt.net/api/private/favorite/
    Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb

Create a new favorite from the private API:

POST https://snipt.net/api/private/favorite/
     Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb
     Content-Type  application/json
     {"snipt": 1, "user": 9971}
  • Note: you can't create duplicate snipt / user favorites, you'll receive an error if you try to do so.
  • snipt is the ID of the snipt to be favorited, and user is the ID of the user who is doing the favoriting.

Get a favorite from the private API:

GET https://snipt.net/api/private/favorite/1948/
    Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb
  • This obviously has limited use. You'll probably want to get a list of the user's favorites (see above) and cache that data.

Delete a favorite from the private API:

DELETE https://snipt.net/api/private/favorite/1948/
       Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb
https://snipt.net/embed/a751a1bcb4a56a0f76e9a119c17d7f9b/
https://snipt.net/raw/a751a1bcb4a56a0f76e9a119c17d7f9b/
a751a1bcb4a56a0f76e9a119c17d7f9b
markdown
Markdown
266
2014-04-17T20:13:14
True
False
Sep 20, 2013 at 02:18 PM
/api/public/snipt/39520/
snipt-api
<p>The Snipt API provides all of the functionality needed to interact with public and private snipts. The entire Snipt UI is actually powered by the API behind the scenes. If you have an issue with the API, you have a few options:</p> <ul> <li>Ask "nicksergeant" in #snipt on irc.freenode.net.</li> <li>Use the comments attached to this snipt.</li> <li>Reply to us on <a href="http://twitter.com/snipt">Twitter</a>.</li> <li>File a support ticket on <a href="https://github.com/nicksergeant/snipt/issues">GitHub</a></li> </ul> <hr /> <h2>Overview</h2> <p>The API is made up of a collection of resources. These resources are separated by whether they are public or private resources. There are two main API endpoints:</p> <blockquote> <p><a href="https://snipt.net/api/public/">https://snipt.net/api/public/</a></p> <p><a href="https://snipt.net/api/private/">https://snipt.net/api/private/</a></p> </blockquote> <p>These endpoints return data in the format based on an HTTP header that you send along in the request. For example, if you would like XML returned:</p> <pre><code>Accept: application/xml </code></pre> <p>The following content types are supported: <code>json</code>, <code>jsonp</code>, <code>xml</code>, <code>yaml</code>, <code>plist</code>.</p> <p>To preview an API response in your browser without sending along a custom HTTP request header, you can append the requested content type in the query string, like so:</p> <blockquote> <p><a href="https://snipt.net/api/public/?format=json">https://snipt.net/api/public/?format=json</a></p> </blockquote> <hr /> <h2>Public API</h2> <p>The public API is simply that: public. It's restricted to GET requests, only. You make requests, get data back, do something with that data, etc.</p> <p>When you get snipts from the public API, you only get snipts back that have been marked as public. Same with tag counts - those counts will only reflect snipts that've been tagged and are also public.</p> <hr /> <h2>Private API</h2> <p>The private API allows you to write apps that interact with a user's snipts. It supports almost everything that the Snipt website supports, as the website itself uses the API as its backend.</p> <h4>Authentication</h4> <p>To authenticate with the private API, you have two options. The first option is to pass an <code>Authorization</code> header along with your request, like this:</p> <pre><code>Authorization: ApiKey username:api_key </code></pre> <p>We've created a test user for testing the private API:</p> <blockquote> <p>Username: <code>api_test_user</code></p> <p>API key: <code>a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb</code></p> </blockquote> <p>The second option for authenticating with the private API is to pass along the username and API key in the query string, like this:</p> <blockquote> <p><a href="https://snipt.net/api/private/?username=api_test_user&amp;api_key=a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb&amp;format=json">https://snipt.net/api/private/?username=api_test_user&amp;api_key=a0eb...</a></p> </blockquote> <hr /> <h2>Resources</h2> <h4>Snipt</h4> <p>Get public snipts:</p> <pre><code>GET https://snipt.net/api/public/snipt/ </code></pre> <ul> <li>Optional parameters: <code>limit</code>, <code>offset</code>, <code>order_by</code>, <code>tag</code>, <code>user</code>, <code>q</code></li> <li>If provided, <code>order_by</code> can be one of: <ul> <li><code>created</code> (oldest first)</li> <li><code>-created</code> (newest first)</li> <li><code>modified</code> (oldest first)</li> <li><code>-modified</code> (newest first)</li> </ul></li> <li>If provided, <code>user</code> must be the ID (integer) of a user, which you can retrieve via a Snipt or User resource (see examples below). Providing a user flag will only return snipts created by that user.</li> <li>If provided, <code>tag</code> must be the ID (integer) of a tag, which you can retrieve via a Snipt or Tag resource (see examples below). Providing a tag flag will only return snipts tagged with that tag.</li> <li>If provided, <code>q</code> is a search term to filter results by.</li> </ul> <hr /> <p>Get public snipts tagged with <code>php</code>:</p> <pre><code>GET https://snipt.net/api/public/snipt/?tag=9 </code></pre> <ul> <li>Note: you can get the ID of a tag from the Tag resource below.</li> </ul> <hr /> <p>Get public snipts ordered by oldest-first:</p> <pre><code>GET https://snipt.net/api/public/snipt/?order_by=created </code></pre> <hr /> <p>Search for public snipts that match <code>flappers</code>:</p> <pre><code>GET https://snipt.net/api/public/snipt/?q=flappers </code></pre> <hr /> <p>Get public snipts created by a specific user:</p> <pre><code>GET https://snipt.net/api/public/snipt/?user=3 </code></pre> <ul> <li>You can get the ID of a user from the User resource or within a Snipt resource.</li> </ul> <hr /> <p>Get an individual snipt:</p> <pre><code>GET https://snipt.net/api/public/snipt/39388/?format=json </code></pre> <hr /> <p>Get a user's snipts from the private API:</p> <pre><code>GET https://snipt.net/api/private/snipt/ Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb </code></pre> <ul> <li>Optional parameters: <code>limit</code>, <code>offset</code>, <code>order_by</code>, <code>tag</code>, <code>q</code></li> <li>If provided, <code>order_by</code> can be one of: <ul> <li><code>created</code> (oldest first)</li> <li><code>-created</code> (newest first)</li> <li><code>modified</code> (oldest first)</li> <li><code>-modified</code> (newest first)</li> </ul></li> <li>If provided, <code>tag</code> must be the ID (integer) of a tag, which you can retrieve via a Snipt or Tag resource (see examples below). Providing a tag flag will only return snipts tagged with that tag.</li> <li>If provided, <code>q</code> is a search term to filter results by.</li> </ul> <hr /> <p>Get a user's individual snipt from the private API:</p> <pre><code>GET https://snipt.net/api/private/snipt/39597/ Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb </code></pre> <hr /> <p>Create a snipt:</p> <pre><code>POST https://snipt.net/api/private/snipt/ Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb Content-Type application/json {"title": "A snipt", "lexer": "text"} </code></pre> <ul> <li>Required fields: <code>lexer</code></li> <li>Optional fields: <code>title</code>, <code>code</code>, <code>tags</code>, <code>public</code>, <code>blog_post</code></li> <li><code>lexer</code> must be one of these (the short / lowercase code): <a href="https://snipt.net/api/public/lexer/">https://snipt.net/api/public/lexer/</a></li> <li>If provided, <code>tags</code> is a string of tags, like this: <code>tag-1, tag2, "tag 3"</code>, etc.</li> </ul> <hr /> <p>Edit a snipt (partial):</p> <pre><code>PATCH https://snipt.net/api/private/snipt/39598/ Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb Content-Type application/json {"title": "A snipt"} </code></pre> <ul> <li>Note: you can also use <code>PUT</code>, but you'll need to send along the entire snipt object, or omitted fields will be erased.</li> </ul> <hr /> <p>Delete a snipt:</p> <pre><code>DELETE https://snipt.net/api/private/snipt/39595/ Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb </code></pre> <hr /> <h4>Lexer</h4> <p>Get all lexers:</p> <pre><code>GET https://snipt.net/api/public/lexer/ </code></pre> <hr /> <h4>User</h4> <p>Get public information on a specific user:</p> <pre><code>GET https://snipt.net/api/public/user/3/ </code></pre> <p>If you don't know the ID:</p> <pre><code>GET https://snipt.net/api/public/user/?username=nick </code></pre> <hr /> <p>Get private information on a specific user from the private API:</p> <pre><code>GET https://snipt.net/api/private/user/9971/ Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb </code></pre> <hr /> <h4>Tag</h4> <p>Get public tags:</p> <pre><code>GET https://snipt.net/api/public/tag/ </code></pre> <ul> <li>This will only retrieve tags that have been assigned to a public snipt.</li> <li>If provided, <code>q</code> is a search term to filter results by. Give it the tag slug, such as: <code>https://snipt.net/api/public/tag/?q=php</code></li> </ul> <hr /> <p>Get a user's tags from the private API:</p> <pre><code>GET https://snipt.net/api/private/tag/ Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb </code></pre> <ul> <li>This will return all tags that have been used by this user.</li> </ul> <hr /> <h4>Favorite</h4> <p>Favorites are only available via the private API.</p> <hr /> <p>Get a user's favorites from the private API:</p> <pre><code>GET https://snipt.net/api/private/favorite/ Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb </code></pre> <hr /> <p>Create a new favorite from the private API:</p> <pre><code>POST https://snipt.net/api/private/favorite/ Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb Content-Type application/json {"snipt": 1, "user": 9971} </code></pre> <ul> <li>Note: you can't create duplicate snipt / user favorites, you'll receive an error if you try to do so.</li> <li><code>snipt</code> is the ID of the snipt to be favorited, and <code>user</code> is the ID of the user who is doing the favoriting.</li> </ul> <hr /> <p>Get a favorite from the private API:</p> <pre><code>GET https://snipt.net/api/private/favorite/1948/ Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb </code></pre> <ul> <li>This obviously has limited use. You'll probably want to get a list of the user's favorites (see above) and cache that data.</li> </ul> <hr /> <p>Delete a favorite from the private API:</p> <pre><code>DELETE https://snipt.net/api/private/favorite/1948/ Authorization ApiKey api_test_user:a0eb5dfb1e8c26366b9b5cfea3f7dc20543bf1fb </code></pre>
api, snipt