Postia API
UK
Консоль

Швидкий старт

Від нуля до опублікованого посту. Потрібен акаунт Postia принаймні з одним підключеним каналом.

1. Створіть ключ

Відкрийте консоль і увійдіть через свій акаунт Postia. Для live-ключів має бути ввімкнено двоетапний вхід. Створіть ключ з потрібними scope і скопіюйте його: він показується один раз. Тестові ключі починаються з pst_test_, live-ключі з pst_live_.

2. Перевірте ключ

curl -X GET https://pub.postia.app/v1/me \
  -H "Authorization: Bearer $POSTIA_API_KEY"
const res = await fetch("https://pub.postia.app/v1/me", {
  method: "GET",
  headers: {
    Authorization: `Bearer ${process.env.POSTIA_API_KEY}`,
  },
});
const data = await res.json();
import os, requests

res = requests.get(
    "https://pub.postia.app/v1/me",
    headers={
        "Authorization": f"Bearer {os.environ['POSTIA_API_KEY']}",
    },
)
data = res.json()
using System.Net.Http.Headers;

var http = new HttpClient();
http.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("POSTIA_API_KEY"));
var req = new HttpRequestMessage(HttpMethod.Get, "https://pub.postia.app/v1/me");
var res = await http.SendAsync(req);
var data = await res.Content.ReadAsStringAsync();
import java.net.URI;
import java.net.http.*;

var req = HttpRequest.newBuilder(URI.create("https://pub.postia.app/v1/me"))
    .header("Authorization", "Bearer " + System.getenv("POSTIA_API_KEY"))
    .method("GET", HttpRequest.BodyPublishers.noBody())
    .build();
var res = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
String data = res.body();
<?php
$ch = curl_init('https://pub.postia.app/v1/me');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('POSTIA_API_KEY'),
    ],
]);
$data = json_decode(curl_exec($ch), true);
req, _ := http.NewRequest("GET", "https://pub.postia.app/v1/me", nil)
req.Header.Set("Authorization", "Bearer "+os.Getenv("POSTIA_API_KEY"))
res, err := http.DefaultClient.Do(req)
if err != nil {
	panic(err)
}
defer res.Body.Close()
data, _ := io.ReadAll(res.Body)
require 'net/http'
require 'json'

uri = URI('https://pub.postia.app/v1/me')
req = Net::HTTP::Get.new(uri)
req['Authorization'] = "Bearer #{ENV['POSTIA_API_KEY']}"
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |h| h.request(req) }
data = JSON.parse(res.body)

У відповіді вказано акаунт, scope й ліміт запитів ключа та баланс ваших кредитів.

3. Знайдіть id каналу

curl -X GET https://pub.postia.app/v1/channels \
  -H "Authorization: Bearer $POSTIA_API_KEY"
const res = await fetch("https://pub.postia.app/v1/channels", {
  method: "GET",
  headers: {
    Authorization: `Bearer ${process.env.POSTIA_API_KEY}`,
  },
});
const data = await res.json();
import os, requests

res = requests.get(
    "https://pub.postia.app/v1/channels",
    headers={
        "Authorization": f"Bearer {os.environ['POSTIA_API_KEY']}",
    },
)
data = res.json()
using System.Net.Http.Headers;

var http = new HttpClient();
http.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("POSTIA_API_KEY"));
var req = new HttpRequestMessage(HttpMethod.Get, "https://pub.postia.app/v1/channels");
var res = await http.SendAsync(req);
var data = await res.Content.ReadAsStringAsync();
import java.net.URI;
import java.net.http.*;

var req = HttpRequest.newBuilder(URI.create("https://pub.postia.app/v1/channels"))
    .header("Authorization", "Bearer " + System.getenv("POSTIA_API_KEY"))
    .method("GET", HttpRequest.BodyPublishers.noBody())
    .build();
var res = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
String data = res.body();
<?php
$ch = curl_init('https://pub.postia.app/v1/channels');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('POSTIA_API_KEY'),
    ],
]);
$data = json_decode(curl_exec($ch), true);
req, _ := http.NewRequest("GET", "https://pub.postia.app/v1/channels", nil)
req.Header.Set("Authorization", "Bearer "+os.Getenv("POSTIA_API_KEY"))
res, err := http.DefaultClient.Do(req)
if err != nil {
	panic(err)
}
defer res.Body.Close()
data, _ := io.ReadAll(res.Body)
require 'net/http'
require 'json'

uri = URI('https://pub.postia.app/v1/channels')
req = Net::HTTP::Get.new(uri)
req['Authorization'] = "Bearer #{ENV['POSTIA_API_KEY']}"
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |h| h.request(req) }
data = JSON.parse(res.body)

Потрібен scope channels:read. Канали підключаються в застосунку Postia, а не через API.

4. Опублікуйте пост

curl -X POST https://pub.postia.app/v1/posts \
  -H "Authorization: Bearer $POSTIA_API_KEY" \
  -H "Idempotency-Key: first-post-1" \
  -H "Content-Type: application/json" \
  -d '{"text": "Hello from the API", "channel_ids": ["CHANNEL_ID"], "publish": true}'
const res = await fetch("https://pub.postia.app/v1/posts", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.POSTIA_API_KEY}`,
    "Idempotency-Key": "first-post-1",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "text": "Hello from the API",
    "channel_ids": [
      "CHANNEL_ID"
    ],
    "publish": true
  }),
});
const data = await res.json();
import os, requests

res = requests.post(
    "https://pub.postia.app/v1/posts",
    headers={
        "Authorization": f"Bearer {os.environ['POSTIA_API_KEY']}",
        "Idempotency-Key": "first-post-1",
    },
    json={
        "text": "Hello from the API",
        "channel_ids": [
            "CHANNEL_ID"
        ],
        "publish": True
    },
)
data = res.json()
using System.Net.Http.Headers;

var http = new HttpClient();
http.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("POSTIA_API_KEY"));
var req = new HttpRequestMessage(HttpMethod.Post, "https://pub.postia.app/v1/posts");
req.Headers.Add("Idempotency-Key", "first-post-1");
req.Content = new StringContent("{\"text\": \"Hello from the API\", \"channel_ids\": [\"CHANNEL_ID\"], \"publish\": true}", System.Text.Encoding.UTF8, "application/json");
var res = await http.SendAsync(req);
var data = await res.Content.ReadAsStringAsync();
import java.net.URI;
import java.net.http.*;

var req = HttpRequest.newBuilder(URI.create("https://pub.postia.app/v1/posts"))
    .header("Authorization", "Bearer " + System.getenv("POSTIA_API_KEY"))
    .header("Idempotency-Key", "first-post-1")
    .header("Content-Type", "application/json")
    .method("POST", HttpRequest.BodyPublishers.ofString("{\"text\": \"Hello from the API\", \"channel_ids\": [\"CHANNEL_ID\"], \"publish\": true}"))
    .build();
var res = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
String data = res.body();
<?php
$ch = curl_init('https://pub.postia.app/v1/posts');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('POSTIA_API_KEY'),
        'Idempotency-Key: first-post-1',
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => '{"text": "Hello from the API", "channel_ids": ["CHANNEL_ID"], "publish": true}',
]);
$data = json_decode(curl_exec($ch), true);
req, _ := http.NewRequest("POST", "https://pub.postia.app/v1/posts", strings.NewReader(`{"text": "Hello from the API", "channel_ids": ["CHANNEL_ID"], "publish": true}`))
req.Header.Set("Authorization", "Bearer "+os.Getenv("POSTIA_API_KEY"))
req.Header.Set("Idempotency-Key", "first-post-1")
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
	panic(err)
}
defer res.Body.Close()
data, _ := io.ReadAll(res.Body)
require 'net/http'
require 'json'

uri = URI('https://pub.postia.app/v1/posts')
req = Net::HTTP::Post.new(uri)
req['Authorization'] = "Bearer #{ENV['POSTIA_API_KEY']}"
req['Idempotency-Key'] = 'first-post-1'
req['Content-Type'] = 'application/json'
req.body = '{"text": "Hello from the API", "channel_ids": ["CHANNEL_ID"], "publish": true}'
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |h| h.request(req) }
data = JSON.parse(res.body)

Не вказуйте publish, щоб отримати чернетку, або передайте schedule_at (ISO 8601 зі зсувом), щоб запланувати пост. Idempotency-Key робить повторний запит безпечним.

5. Стежте за результатом

Доставка асинхронна. Прочитайте пост через GET /v1/posts/{post_id}, щоб побачити результат по кожному каналу, або підпишіться на webhook.