Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

API Docs

Nick Sergeant edited this page Apr 30, 2019 · 4 revisions

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:


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
Clone this wiki locally