Telegraph API

Telegraph API


Telegra.ph is a minimalist publishing tool that allows you to create richly formatted posts and push them to the Web in just a click. Telegraph posts also get beautiful Instant View pages on Telegram.

To maintain the purity of the basic interface, we launched the @Telegraph bot for those who require advanced features. This bot can help you manage your articles across any number of devices and get page view statistics for any Telegraph page.

Anyone can enjoy the simplicity of Telegraph publishing, not just Telegram users. For this reason, all developers are welcome to use this Telegraph API to create bots like @Telegraph for any other platform, or even standalone interfaces.


All queries to the Telegraph API must be served over HTTPS and should be presented in this form: https://api.telegra.ph/%method%.

If a path parameter is present, you can also use this form: https://api.telegra.ph/%method%/%path%.

1. Methods

2. Types

3. Content format


Available methods

We support GET and POST HTTP methods. The response contains a JSON object, which always has a Boolean field ok. If ok equals true, the request was successful, and the result of the query can be found in the result field. In case of an unsuccessful request, ok equals false, and the error is explained in the error field (e.g. SHORT_NAME_REQUIRED). All queries must be made using UTF-8.

createAccount

Use this method to create a new Telegraph account. Most users only need one account, but this can be useful for channel administrators who would like to keep individual author names and profile links for each of their channels. On success, returns an Account object with the regular fields and an additional access_token field.

  • short_name (String, 1-32 characters)
    Required. Account name, helps users with several accounts remember which they are currently using. Displayed to the user above the "Edit/Publish" button on Telegra.ph, other users don't see this name.
  • author_name (String, 0-128 characters)
    Default author name used when creating new articles.
  • author_url (String, 0-512 characters)
    Default profile link, opened when users click on the author's name below the title. Can be any link, not necessarily to a Telegram profile or channel.
Sample request
https://api.telegra.ph/createAccount?short_name=Sandbox&author_name=Anonymous

editAccountInfo

Use this method to update information about a Telegraph account. Pass only the parameters that you want to edit. On success, returns an Account object with the default fields.

  • access_token (String)
    Required. Access token of the Telegraph account.
  • short_name (String, 1-32 characters)
    New account name.
  • author_name (String, 0-128 characters)
    New default author name used when creating new articles.
  • author_url (String, 0-512 characters)
    New default profile link, opened when users click on the author's name below the title. Can be any link, not necessarily to a Telegram profile or channel.
Sample request
https://api.telegra.ph/editAccountInfo?access_token=b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb&short_name=Sandbox&author_name=Anonymous

getAccountInfo

Use this method to get information about a Telegraph account. Returns an Account object on success.

  • access_token (String)
    Required. Access token of the Telegraph account.
  • fields (Array of String, default = [“short_name”,“author_name”,“author_url”])
    List of account fields to return. Available fields: short_name, author_name, author_url, auth_url, page_count.
Sample request
https://api.telegra.ph/getAccountInfo?access_token=b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb&fields=["short_name","page_count"]

revokeAccessToken

Use this method to revoke access_token and generate a new one, for example, if the user would like to reset all connected sessions, or you have reasons to believe the token was compromised. On success, returns an Account object with new access_token and auth_url fields.

  • access_token (String)
    Required. Access token of the Telegraph account.
Sample request
https://api.telegra.ph/revokeAccessToken?access_token=b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb

createPage

Use this method to create a new Telegraph page. On success, returns a Page object.

  • access_token (String)
    Required. Access token of the Telegraph account.
  • title (String, 1-256 characters)
    Required. Page title.
  • author_name (String, 0-128 characters)
    Author name, displayed below the article's title.
  • author_url (String, 0-512 characters)
    Profile link, opened when users click on the author's name below the title. Can be any link, not necessarily to a Telegram profile or channel.
  • content (Array of Node, up to 64 KB)
    Required. Content of the page.
  • return_content (Boolean, default = false)
    If true, a content field will be returned in the Page object (see: Content format).
Sample request
https://api.telegra.ph/createPage?access_token=b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb&title=Sample+Page&author_name=Anonymous&content=[{"tag":"p","children":["Hello,+world!"]}]&return_content=true

editPage

Use this method to edit an existing Telegraph page. On success, returns a Page object.

  • access_token (String)
    Required. Access token of the Telegraph account.
  • path (String)
    Required. Path to the page.
  • title (String, 1-256 characters)
    Required. Page title.
  • content (Array of Node, up to 64 KB)
    Required. Content of the page.
  • author_name (String, 0-128 characters)
    Author name, displayed below the article's title.
  • author_url (String, 0-512 characters)
    Profile link, opened when users click on the author's name below the title. Can be any link, not necessarily to a Telegram profile or channel.
  • return_content (Boolean, default = false)
    If true, a content field will be returned in the Page object.
Sample request
https://api.telegra.ph/editPage/Sample-Page-12-15?access_token=b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb&title=Sample+Page&author_name=Anonymous&content=[{"tag":"p","children":["Hello,+world!"]}]&return_content=true

getPage

Use this method to get a Telegraph page. Returns a Page object on success.

  • path (String)
    Required. Path to the Telegraph page (in the format Title-12-31, i.e. everything that comes after http://telegra.ph/).
  • return_content (Boolean, default = false)
    If true, content field will be returned in Page object.
Sample request
https://api.telegra.ph/getPage/Sample-Page-12-15?return_content=true

getPageList

Use this method to get a list of pages belonging to a Telegraph account. Returns a PageList object, sorted by most recently created pages first.

  • access_token (String)
    Required. Access token of the Telegraph account.
  • offset (Integer, default = 0)
    Sequential number of the first page to be returned.
  • limit (Integer, 0-200, default = 50)
    Limits the number of pages to be retrieved.
Sample request
https://api.telegra.ph/getPageList?access_token=b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb&limit=3

getViews

Use this method to get the number of views for a Telegraph article. Returns a PageViews object on success. By default, the total number of page views will be returned.

  • path (String)
    Required. Path to the Telegraph page (in the format Title-12-31, where 12 is the month and 31 the day the article was first published).
  • year (Integer, 2000-2100)
    Required if month is passed. If passed, the number of page views for the requested year will be returned.
  • month (Integer, 1-12)
    Required if day is passed. If passed, the number of page views for the requested month will be returned.
  • day (Integer, 1-31)
    Required if hour is passed. If passed, the number of page views for the requested day will be returned.
  • hour (Integer, 0-24)
    If passed, the number of page views for the requested hour will be returned.
Sample request
https://api.telegra.ph/getViews/Sample-Page-12-15?year=2016&month=12

Available types

All types used in the Telegraph API responses are represented as JSON-objects. Optional fields may be not returned when irrelevant.

Account

This object represents a Telegraph account.

  • short_name (String)
    Account name, helps users with several accounts remember which they are currently using. Displayed to the user above the "Edit/Publish" button on Telegra.ph, other users don't see this name.
  • author_name (String)
    Default author name used when creating new articles.
  • author_url (String)
    Profile link, opened when users click on the author's name below the title. Can be any link, not necessarily to a Telegram profile or channel.
  • access_token (String)
    Optional. Only returned by the createAccount and revokeAccessToken method. Access token of the Telegraph account.
  • auth_url (String)
    Optional. URL to authorize a browser on telegra.ph and connect it to a Telegraph account. This URL is valid for only one use and for 5 minutes only.
  • page_count (Integer)
    Optional. Number of pages belonging to the Telegraph account.

PageList

This object represents a list of Telegraph articles belonging to an account. Most recently created articles first.

  • total_count (Integer)
    Total number of pages belonging to the target Telegraph account.
  • pages (Array of Page)
    Requested pages of the target Telegraph account.

Page

This object represents a page on Telegraph.

  • path (String)
    Path to the page.
  • url (String)
    URL of the page.
  • title (String)
    Title of the page.
  • description (String)
    Description of the page.
  • author_name (String)
    Optional. Name of the author, displayed below the title.
  • author_url (String)
    Optional. Profile link, opened when users click on the author's name below the title. Can be any link, not necessarily to a Telegram profile or channel.
  • image_url (String)
    Optional. Image URL of the page.
  • content (Array of Node)
    OptionalContent of the page.
  • views (Integer)
    Number of page views for the page.
  • can_edit (Boolean)
    Optional. Only returned if access_token passedTrue, if the target Telegraph account can edit the page.

PageViews

This object represents the number of page views for a Telegraph article.

  • views (Integer)
    Number of page views for the target page.

Node

This abstract object represents a DOM Node. It can be a String which represents a DOM text node or a NodeElement object.

NodeElement

This object represents a DOM element node.

  • tag (String)
    Name of the DOM element. Available tags: a, aside, b, blockquote, br, code, em, figcaption, figure, h3, h4, hr, i, iframe, img, li, ol, p, pre, s, strong, u, ul, video.
  • attrs (Object)
    Optional. Attributes of the DOM element. Key of object represents name of attribute, value represents value of attribute. Available attributes: href, src.
  • children (Array of Node)
    Optional. List of child nodes for the DOM element.

Content format

The Telegraph API uses a DOM-based format to represent the content of the page. Below is an example of code in javascript which explains how you can use it:

function domToNode(domNode) {
if (domNode.nodeType == domNode.TEXT_NODE) {
return domNode.data;
}
if (domNode.nodeType != domNode.ELEMENT_NODE) {
  return false;
}
  var nodeElement = {};
  nodeElement.tag = domNode.tagName.toLowerCase();
  for (var i = 0; i < domNode.attributes.length; i++) {
   var attr = domNode.attributes[i];
   if (attr.name == 'href' || attr.name == 'src') {
    if (!nodeElement.attrs) {
     nodeElement.attrs = {};
    }
    nodeElement.attrs[attr.name] = attr.value;
   }
  }
  if (domNode.childNodes.length > 0) {
  nodeElement.children = [];
  for (var i = 0; i < domNode.childNodes.length; i++) {
    var child = domNode.childNodes[i];
    nodeElement.children.push(domToNode(child));
  }
  }
  return nodeElement;
}

function nodeToDom(node) {
if (typeof node === 'string' || node instanceof String) {
  return document.createTextNode(node);
}
if (node.tag) {
  var domNode = document.createElement(node.tag);
  if (node.attrs) {
    for (var name in node.attrs) {
    var value = node.attrs[name];
    domNode.setAttribute(name, value);
  }
  }
} else {
  var domNode = document.createDocumentFragment();
}
  if (node.children) {
   for (var i = 0; i < node.children.length; i++) {
    var child = node.children[i];
    domNode.appendChild(nodeToDom(child));
   }
  }
  return domNode;
}

var article = document.getElementById('article');
var content = domToNode(article).children;
$.ajax('https://api.telegra.ph/createPage', {
  data: {
  access_token:  '%access_token%',
  title:      'Title of page',
  content:     JSON.stringify(content),
  return_content: true
},
type: 'POST',
  dataType: 'json',
  success: function(data) {
  if (data.content) {
    while (article.firstChild) {
    article.removeChild(article.firstChild);
    }
    article.appendChild(nodeToDom({children: data.content}));
  }
  }
});