Sponsored By

















## 1. 노션 API 등록/설정


1-1. 아래 주소 클릭
https://www.notion.so/my-integrations


1-2. 새 API 통합 클릭




1-3. 연결된 워크스페이스를 확인하고 이름을 만들어주고 ‘제출’ 버튼 클릭






1-4. 시크릿키가 생성되면 표시 > 복사 를 순서대로 클릭하여 키값을 복사한다.






※ 시크릿키값은 secret_* 의 형태로 되어있다.


 



## 2. 노션에서 사용/연동 할 데이터베이스 생성


2-1. 아래와 같이 샘플용으로 만들어본다.





2-2. 테이블에 마우스를 가져가 되면 옆에 더보기(::)가 표시되는데 클릭한다.


2-3. 링크복사를 누른다
https://www.notion.so/asteriskos/de2dcf74aff44030954dbd4d991c559b?v=1f7e542c866341c1a8d68c9c328c3faf&pvs=4

표시한 부분이 데이터베이스의 ID 이다. 



## 3. 데이터베이스 페이지에 API연동


3-1. 데이터베이스 생성한 페이지 우측상단에  더보기(…) 를 클릭하여 API를 연동한다.






3-2. 연결할거냐는 팝업이뜨는데, ‘확인’ 버튼을 눌러 연결됨을 확인하자.






## 4. 파이썬 코딩하여 노션 데이터베이스에 데이터 넣기


4-1. 데이터베이스 페이지 가져오기

import requests, json

def readDatabase(databaseId, token):
    
    readUrl = f"https://api.notion.com/v1/databases/{databaseId}/query"
headers = {'Content-Type': 'application/json', "Authorization": 'Bearer ' + token, "Notion-Version": '2022-06-28'}
    res = requests.post(readUrl, headers=headers)
    data = res.json()
 
    if res.status_code == 200:
        for result in data['results']:
            try:
                properties = result['properties']
col1 = properties['Name']['title'][0]['text']['content']
                col2 = properties['금액']['number']
                print(f'Name: {col1}, 금액: {col2}')
            except:
                continue
        
token = "{Your Private API Token}"

databaseId = "{Your DatabaseID}"

readDatabase(databaseId=databaseId, token=token)




4-2. 적용>실행 후 데이터 불러온 값 확인하기(결과)






4-3. 노션 데이터베이스에 페이지 추가하기

import requests, json


def InsertData(databaseId, token, dataArray):
    createdUrl = "https://api.notion.com/v1/pages"
    headers = {'Content-Type': 'application/json', "Authorization": 'Bearer ' + token, "Notion-Version": '2022-06-28'}

    data = {
        "parent": {"database_id": databaseId},
        "properties": {
            "Name": {
                "title": [
                    {
                        "text": {
                            "content": dataArray['Name']
                        }
                    }
                ]
            },
            "금액": {
                "number": dataArray['금액']
            }
        }
    }

    data = json.dumps(data)
    res = requests.post(createdUrl, headers=headers, data=data)
    if res.status_code == 200:
    print('## 정상입력 ##')
  else:
    print('## 입력에러 ##')   


token = "{Your Private API Token}"

databaseId = "{Your DatabaseID}"


dataArray = {
    'Name': 'Test_789',
    '금액': 123333
}

InsertData(databaseId=databaseId, token=token, dataArray=dataArray)




4-4. 결과 







Sponsored By















+ Recent posts