import os
import requests
url = "https://api.notion.com/v1/databases"
headers = {
"Authorization": f"Bearer {os.environ['NOTION_API_KEY']}",
"Notion-Version": "2022-06-28",
"Content-Type": "application/json"
}
data = {
"parent": {"type": "workspace"},
"title": [{"type": "title", "text": {"content": "Tasks"}}],
"properties": {
"Name": {"type": "title"},
"Status": {
"type": "select",
"select": {
"options": [
{"name": "Todo", "color": "red"},
{"name": "Done", "color": "green"}
]
}
}
}
}
response = requests.post(url, headers=headers, json=data)
if response.status_code >= 400:
print(response.text)
import os
import httpx
url = "https://api.notion.com/v1/databases"
headers = {
"Authorization": f"Bearer {os.environ['NOTION_API_KEY']}",
"Notion-Version": "2022-06-28",
"Content-Type": "application/json"
}
data = {
"parent": {"type": "workspace"},
"title": [{"type": "title", "text": {"content": "Tasks"}}],
"properties": {
"Name": {"type": "title"},
"Status": {
"type": "select",
"select": {
"options": [
{"name": "Todo", "color": "red"},
{"name": "Done", "color": "green"}
]
}
}
}
}
with httpx.Client() as client:
response = client.post(url, headers=headers, json=data)
if response.status_code >= 400:
print(response.text)
import fetch from 'node-fetch';
import { config } from 'dotenv';
config();
const url = 'https://api.notion.com/v1/databases';
const headers = {
'Authorization': `Bearer ${process.env.NOTION_API_KEY}`,
'Notion-Version': '2022-06-28',
'Content-Type': 'application/json'
};
const data = {
parent: { type: 'workspace' },
title: [{ type: 'title', text: { content: 'Tasks' } }],
properties: {
Name: { type: 'title' },
Status: {
type: 'select',
select: {
options: [
{ name: 'Todo', color: 'red' },
{ name: 'Done', color: 'green' }
]
}
}
}
};
fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(data)
})
.then(res => {
if (res.status >= 400) {
return res.text().then(text => console.error(text));
}
return res.json();
})
.then(console.log)
.catch(console.error);
import axios from 'axios';
import { config } from 'dotenv';
config();
const url = 'https://api.notion.com/v1/databases';
const headers = {
'Authorization': `Bearer ${process.env.NOTION_API_KEY}`,
'Notion-Version': '2022-06-28',
'Content-Type': 'application/json'
};
const data = {
parent: { type: 'workspace' },
title: [{ type: 'title', text: { content: 'Tasks' } }],
properties: {
Name: { type: 'title' },
Status: {
type: 'select',
select: {
options: [
{ name: 'Todo', color: 'red' },
{ name: 'Done', color: 'green' }
]
}
}
}
};
axios.post(url, data, { headers })
.then(res => {
if (res.status >= 400) {
console.error(res.data);
}
})
.catch(console.error);
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
type DatabaseRequest struct {
Parent map[string]interface{} `json:"parent"`
Title []map[string]interface{} `json:"title"`
Properties map[string]interface{} `json:"properties"`
}
func main() {
url := "https://api.notion.com/v1/databases"
req, err := http.NewRequest("POST", url, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("NOTION_API_KEY"))
req.Header.Set("Notion-Version", "2022-06-28")
req.Header.Set("Content-Type", "application/json")
data := DatabaseRequest{
Parent: map[string]interface{}{"type": "workspace"},
Title: []map[string]interface{}{
{"type": "title", "text": map[string]interface{}{"content": "Tasks"}},
},
Properties: map[string]interface{}{
"Name": map[string]interface{}{"type": "title"},
"Status": map[string]interface{}{
"type": "select",
"select": map[string]interface{}{
"options": []map[string]interface{}{
{"name": "Todo", "color": "red"},
{"name": "Done", "color": "green"},
},
},
},
},
}
body, err := json.Marshal(data)
if err != nil {
log.Fatal(err)
}
req.Body = io.NopCloser(bytes.NewBuffer(body))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
}
}
<?php
$url = 'https://api.notion.com/v1/databases';
$headers = [
'Authorization: Bearer ' . getenv('NOTION_API_KEY'),
'Notion-Version: 2022-06-28',
'Content-Type: application/json'
];
$data = [
'parent' => ['type' => 'workspace'],
'title' => [['type' => 'title', 'text' => ['content' => 'Tasks']]],
'properties' => [
'Name' => ['type' => 'title'],
'Status' => [
'type' => 'select',
'select' => [
'options' => [
['name' => 'Todo', 'color' => 'red'],
['name' => 'Done', 'color' => 'green']
]
]
]
]
];
$options = [
'http' => [
'method' => 'POST',
'header' => $headers,
'content' => json_encode($data),
'ignore_errors' => true
]
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if ($http_response_header[0] !== 'HTTP/1.1 200 OK') {
echo $response;
}
require 'net/http'
require 'json'
require 'uri'
url = URI.parse('https://api.notion.com/v1/databases')
headers = {
'Authorization' => "Bearer #{ENV['NOTION_API_KEY']}",
'Notion-Version' => '2022-06-28',
'Content-Type' => 'application/json'
}
data = {
parent: { type: 'workspace' },
title: [{ type: 'title', text: { content: 'Tasks' } }],
properties: {
Name: { type: 'title' },
Status: {
type: 'select',
select: {
options: [
{ name: 'Todo', color: 'red' },
{ name: 'Done', color: 'green' }
]
}
}
}
}
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url.path, headers)
request.body = data.to_json
response = http.request(request)
if response.code.to_i >= 400
puts response.body
end
import Foundation
let url = URL(string: "https://api.notion.com/v1/databases")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("Bearer \(ProcessInfo.processInfo.environment["NOTION_API_KEY"] ?? "")", forHTTPHeaderField: "Authorization")
request.setValue("2022-06-28", forHTTPHeaderField: "Notion-Version")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let data = [
"parent": ["type": "workspace"],
"title": [["type": "title", "text": ["content": "Tasks"]]],
"properties": [
"Name": ["type": "title"],
"Status": [
"type": "select",
"select": [
"options": [
["name": "Todo", "color": "red"],
["name": "Done", "color": "green"]
]
]
]
]
]
request.httpBody = try? JSONSerialization.data(withJSONObject: data)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print(error)
return
}
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode >= 400 else {
return
}
if let data = data, let body = String(data: data, encoding: .utf8) {
print(body)
}
}
task.resume()
RunLoop.main.run()
use reqwest::blocking::Client;
use serde_json::json;
use std::env;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let url = "https://api.notion.com/v1/databases";
let api_key = env::var("NOTION_API_KEY")?;
let data = json!({
"parent": {"type": "workspace"},
"title": [{"type": "title", "text": {"content": "Tasks"}}],
"properties": {
"Name": {"type": "title"},
"Status": {
"type": "select",
"select": {
"options": [
{"name": "Todo", "color": "red"},
{"name": "Done", "color": "green"}
]
}
}
}
});
let response = client
.post(url)
.header("Authorization", format!("Bearer {}", api_key))
.header("Notion-Version", "2022-06-28")
.header("Content-Type", "application/json")
.json(&data)
.send()?;
if !response.status().is_success() {
println!("{}", response.text()?);
}
Ok(())
}
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
var client = new HttpClient();
var url = "https://api.notion.com/v1/databases";
var apiKey = Environment.GetEnvironmentVariable("NOTION_API_KEY");
var data = new
{
parent = new { type = "workspace" },
title = new[] { new { type = "title", text = new { content = "Tasks" } } },
properties = new
{
Name = new { type = "title" },
Status = new
{
type = "select",
select = new
{
options = new[]
{
new { name = "Todo", color = "red" },
new { name = "Done", color = "green" }
}
}
}
}
};
var content = new StringContent(JsonSerializer.Serialize(data));
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var request = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = content
};
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
request.Headers.Add("Notion-Version", "2022-06-28");
var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}
}
}