### Install notionapi package Source: https://github.com/jomei/notionapi/blob/main/README.md Use the Go toolchain to download and install the notionapi library into your project dependencies. ```bash go get github.com/jomei/notionapi ``` -------------------------------- ### Initialize Notion API Client Source: https://context7.com/jomei/notionapi/llms.txt Demonstrates how to instantiate the Notion client using an integration token. It covers basic setup, advanced configuration with custom HTTP clients and retry logic, and OAuth credential setup. ```go package main import ( "context" "fmt" "net/http" "time" "github.com/jomei/notionapi" ) func main() { // Basic initialization with just a token client := notionapi.NewClient(notionapi.Token("secret_your_integration_token")) // Advanced initialization with options customHTTPClient := &http.Client{ Timeout: 30 * time.Second, } client = notionapi.NewClient( notionapi.Token("secret_your_integration_token"), notionapi.WithHTTPClient(customHTTPClient), notionapi.WithVersion("2022-06-28"), notionapi.WithRetry(5), // Max retry attempts on 429 errors ) // For OAuth applications client = notionapi.NewClient( notionapi.Token(""), notionapi.WithOAuthAppCredentials("client_id", "client_secret"), ) // Access services through the client ctx := context.Background() page, err := client.Page.Get(ctx, "page_id") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Page URL: %s\n", page.URL) } ``` -------------------------------- ### Query Notion Database with Filters and Pagination in Go Source: https://context7.com/jomei/notionapi/llms.txt Demonstrates how to perform database queries using the jomei/notionapi client. Includes examples for basic property filtering, sorting, pagination via cursors, and complex compound filters. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() // Simple query with property filter response, err := client.Database.Query(ctx, notionapi.DatabaseID("database_id"), ¬ionapi.DatabaseQueryRequest{ Filter: notionapi.PropertyFilter{ Property: "Status", Select: ¬ionapi.SelectFilterCondition{ Equals: "In Progress", }, }, Sorts: []notionapi.SortObject{ { Property: "Created", Direction: notionapi.SortOrderDESC, }, }, PageSize: 50, }) if err != nil { fmt.Printf("Error: %v\n", err) return } // Process results for _, page := range response.Results { fmt.Printf("Page ID: %s\n", page.ID) } // Handle pagination if response.HasMore { nextResponse, _ := client.Database.Query(ctx, notionapi.DatabaseID("database_id"), ¬ionapi.DatabaseQueryRequest{ StartCursor: response.NextCursor, PageSize: 50, }) fmt.Printf("Next batch: %d results\n", len(nextResponse.Results)) } // Compound filter with AND andFilter := notionapi.AndCompoundFilter{ notionapi.PropertyFilter{ Property: "Status", Select: ¬ionapi.SelectFilterCondition{Equals: "Done"}, }, notionapi.PropertyFilter{ Property: "Priority", Select: ¬ionapi.SelectFilterCondition{Equals: "High"}, }, } _, err = client.Database.Query(ctx, notionapi.DatabaseID("database_id"), ¬ionapi.DatabaseQueryRequest{ Filter: andFilter, }) // Compound filter with OR orFilter := notionapi.OrCompoundFilter{ notionapi.PropertyFilter{ Property: "Tags", MultiSelect: ¬ionapi.MultiSelectFilterCondition{Contains: "urgent"}, }, notionapi.PropertyFilter{ Property: "Tags", MultiSelect: ¬ionapi.MultiSelectFilterCondition{Contains: "important"}, }, } _, err = client.Database.Query(ctx, notionapi.DatabaseID("database_id"), ¬ionapi.DatabaseQueryRequest{ Filter: orFilter, }) // Timestamp filter timestampFilter := notionapi.TimestampFilter{ Timestamp: notionapi.TimestampCreated, CreatedTime: ¬ionapi.DateFilterCondition{PastWeek: &struct{}{}}, } _, err = client.Database.Query(ctx, notionapi.DatabaseID("database_id"), ¬ionapi.DatabaseQueryRequest{ Filter: timestampFilter, }) } ``` -------------------------------- ### Retrieve Notion Database Source: https://context7.com/jomei/notionapi/llms.txt Shows how to fetch a database object by its ID using the Database service. The example demonstrates accessing database metadata and iterating over property configurations. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() database, err := client.Database.Get(ctx, notionapi.DatabaseID("database_id_here")) if err != nil { fmt.Printf("Error getting database: %v\n", err) return } fmt.Printf("Database: %s\n", database.Title[0].PlainText) fmt.Printf("URL: %s\n", database.URL) fmt.Printf("Created: %s\n", database.CreatedTime) // Iterate through property configurations for name, prop := range database.Properties { fmt.Printf("Property: %s, Type: %s\n", name, prop.GetType()) } } ``` -------------------------------- ### GET /databases/{database_id} Source: https://context7.com/jomei/notionapi/llms.txt Retrieves a database object by its ID, including the schema and property configurations. ```APIDOC ## GET /databases/{database_id} ### Description Retrieves a database by its ID, returning the database schema including all property configurations. This is useful for understanding the structure of a database before querying or creating pages within it. ### Method GET ### Endpoint /databases/{database_id} ### Parameters #### Path Parameters - **database_id** (string) - Required - The unique identifier of the Notion database. ### Request Example ```go client.Database.Get(ctx, notionapi.DatabaseID("database_id_here")) ``` ### Response #### Success Response (200) - **id** (string) - The ID of the database. - **title** (array) - The title of the database. - **properties** (object) - The schema of the database properties. - **url** (string) - The URL of the database in Notion. #### Response Example { "id": "database_id", "title": [{"plain_text": "My Database"}], "url": "https://notion.so/database_id", "properties": {} } ``` -------------------------------- ### GET /pages/{page_id} Source: https://context7.com/jomei/notionapi/llms.txt Retrieves a page object by its ID, providing access to page properties and metadata. ```APIDOC ## GET /pages/{page_id} ### Description Retrieves a page object by its ID. This endpoint returns the page properties, parent information, and metadata. ### Method GET ### Endpoint /pages/{page_id} ### Parameters #### Path Parameters - **page_id** (string) - Required - The unique identifier of the Notion page. ### Request Example ```go client.Page.Get(ctx, "page_id") ``` ### Response #### Success Response (200) - **id** (string) - The ID of the page. - **url** (string) - The URL of the page. - **properties** (object) - The properties of the page. #### Response Example { "id": "page_id", "url": "https://notion.so/page_id", "properties": {} } ``` -------------------------------- ### GET /blocks/{block_id}/children Source: https://context7.com/jomei/notionapi/llms.txt Retrieves all child blocks of a given block or page. This endpoint is used to fetch the content of a page. Results are paginated. ```APIDOC ## GET /blocks/{block_id}/children ### Description Retrieves all child blocks of a block or page. This is how you get page content. Results are paginated. ### Method GET ### Endpoint /v1/blocks/{block_id}/children ### Parameters #### Path Parameters - **block_id** (string) - Required - The unique identifier of the block or page whose children are to be retrieved. #### Query Parameters - **page_size** (integer) - Optional - The number of results to return per page. Maximum is 100. - **start_cursor** (string) - Optional - The cursor to start retrieving results from. Used for pagination. ### Request Example ```json { "example": "Not applicable for GET request" } ``` ### Response #### Success Response (200) - **object** (string) - Always 'list'. - **results** (array) - An array of block objects. - **next_cursor** (string) - The cursor to use to retrieve the next page of results. Null if there are no more pages. - **has_more** (boolean) - True if there are more pages of results, false otherwise. #### Response Example ```json { "object": "list", "results": [ { "object": "block", "id": "f7579610-6931-433a-a717-79112237232f", "type": "heading_2", "heading_2": { "rich_text": [ { "type": "text", "text": { "content": "Page Content", "link": null } } ] }, "created_time": "2021-05-29T21:30:00.000Z", "last_edited_time": "2021-05-29T21:30:00.000Z", "has_children": false } ], "next_cursor": null, "has_more": false } ``` ``` -------------------------------- ### Get Notion Page with Go Source: https://context7.com/jomei/notionapi/llms.txt Retrieves a Notion page by its ID, including all its properties. This function returns page properties, not the page content. For page content, use the Block.GetChildren method. Requires the notionapi Go client. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() page, err := client.Page.Get(ctx, notionapi.PageID("page_id_here")) if err != nil { fmt.Printf("Error getting page: %v\n", err) return } fmt.Printf("Page ID: %s\n", page.ID) fmt.Printf("URL: %s\n", page.URL) fmt.Printf("Created: %s\n", page.CreatedTime) fmt.Printf("Last Edited: %s\n", page.LastEditedTime) fmt.Printf("Archived: %v\n", page.Archived) // Access properties for name, prop := range page.Properties { switch p := prop.(type) { case *notionapi.TitleProperty: if len(p.Title) > 0 { fmt.Printf("%s: %s\n", name, p.Title[0].PlainText) } case *notionapi.SelectProperty: fmt.Printf("%s: %s\n", name, p.Select.Name) case *notionapi.NumberProperty: fmt.Printf("%s: %f\n", name, p.Number) case *notionapi.CheckboxProperty: fmt.Printf("%s: %v\n", name, p.Checkbox) } } } ``` -------------------------------- ### GET /blocks/{block_id} Source: https://context7.com/jomei/notionapi/llms.txt Retrieves a single block by its unique identifier. Blocks represent content elements within Notion pages, such as paragraphs, headings, and list items. ```APIDOC ## GET /blocks/{block_id} ### Description Retrieves a single block by ID. Blocks are the content elements within pages including paragraphs, headings, lists, and more. ### Method GET ### Endpoint /v1/blocks/{block_id} ### Parameters #### Path Parameters - **block_id** (string) - Required - The unique identifier of the block to retrieve. ### Request Example ```json { "example": "Not applicable for GET request" } ``` ### Response #### Success Response (200) - **id** (string) - The unique identifier of the block. - **type** (string) - The type of the block (e.g., 'paragraph', 'heading_1', 'to_do'). - **has_children** (boolean) - Indicates if the block contains child blocks. #### Response Example ```json { "object": "block", "id": "9186431f-0197-4117-811a-90354226541f", "type": "paragraph", "paragraph": { "rich_text": [ { "type": "text", "text": { "content": "This is a paragraph.", "link": null } } ] }, "created_time": "2021-05-29T21:30:00.000Z", "last_edited_time": "2021-05-29T21:30:00.000Z", "has_children": false } ``` ``` -------------------------------- ### Get Comments from Notion Page or Block (Go) Source: https://context7.com/jomei/notionapi/llms.txt Retrieves all unresolved comments from a specific Notion page or block using the Notion API. It supports pagination to fetch comments efficiently. Requires a Notion client and a block ID. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() response, err := client.Comment.Get(ctx, notionapi.BlockID("page_id"), ¬ionapi.Pagination{ PageSize: 50, }) if err != nil { fmt.Printf("Error getting comments: %v\n", err) return } for _, comment := range response.Results { fmt.Printf("Comment ID: %s\n", comment.ID) fmt.Printf("Created by: %s\n", comment.CreatedBy.Name) fmt.Printf("Created: %s\n", comment.CreatedTime) for _, rt := range comment.RichText { fmt.Printf("Content: %s\n", rt.PlainText) } fmt.Println("---") } } ``` -------------------------------- ### Initialize Notion API Client Source: https://github.com/jomei/notionapi/blob/main/README.md Create a new instance of the Notion API client using your integration token. This client serves as the entry point for all subsequent API calls. ```go import "github.com/jomei/notionapi" client := notionapi.NewClient("your_integration_token") ``` -------------------------------- ### OAuth Authentication with Notion API in Go Source: https://context7.com/jomei/notionapi/llms.txt Demonstrates how to create an access token for Notion OAuth integrations using the Go client. It initializes the client with OAuth credentials, exchanges an authorization code for an access token, and then uses the authenticated token to make API calls. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { // Initialize client with OAuth credentials (no token needed initially) client := notionapi.NewClient( notionapi.Token(""), notionapi.WithOAuthAppCredentials("your_client_id", "your_client_secret"), ) ctx := context.Background() // Exchange authorization code for access token tokenResponse, err := client.Authentication.CreateToken(ctx, ¬ionapi.TokenCreateRequest{ Code: "authorization_code_from_oauth_callback", GrantType: "authorization_code", RedirectUri: "https://your-app.com/oauth/callback", }) if err != nil { if tokenErr, ok := err.(*notionapi.TokenCreateError); ok { fmt.Printf("OAuth Error: %s - %s\n", tokenErr.Code, tokenErr.Message) } return } fmt.Printf("Access Token: %s\n", tokenResponse.AccessToken) fmt.Printf("Bot ID: %s\n", tokenResponse.BotId) fmt.Printf("Workspace ID: %s\n", tokenResponse.WorkspaceId) fmt.Printf("Workspace Name: %s\n", tokenResponse.WorkspaceName) // Use the access token to create a new client for API calls authenticatedClient := notionapi.NewClient(notionapi.Token(tokenResponse.AccessToken)) // Now you can make API calls on behalf of the user me, _ := authenticatedClient.User.Me(ctx) fmt.Printf("Authenticated as: %s\n", me.Name) } ``` -------------------------------- ### Create Notion Database with Go Source: https://context7.com/jomei/notionapi/llms.txt Creates a new Notion database as a child of an existing page. Requires specifying the parent page ID and defining the property schema for the database. Uses the notionapi Go client library. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() database, err := client.Database.Create(ctx, ¬ionapi.DatabaseCreateRequest{ Parent: notionapi.Parent{ Type: notionapi.ParentTypePageID, PageID: notionapi.PageID("parent_page_id"), }, Title: []notionapi.RichText{ { Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "Project Tasks"}, }, }, IsInline: false, Properties: notionapi.PropertyConfigs{ "Name": notionapi.TitlePropertyConfig{ Type: notionapi.PropertyConfigTypeTitle, Title: struct{}{}, }, "Status": notionapi.SelectPropertyConfig{ Type: notionapi.PropertyConfigTypeSelect, Select: notionapi.Select{ Options: []notionapi.Option{ {Name: "To Do", Color: notionapi.ColorGray}, {Name: "In Progress", Color: notionapi.ColorBlue}, {Name: "Done", Color: notionapi.ColorGreen}, }, }, }, "Priority": notionapi.SelectPropertyConfig{ Type: notionapi.PropertyConfigTypeSelect, Select: notionapi.Select{ Options: []notionapi.Option{ {Name: "Low", Color: notionapi.ColorGray}, {Name: "Medium", Color: notionapi.ColorYellow}, {Name: "High", Color: notionapi.ColorRed}, }, }, }, "Due Date": notionapi.DatePropertyConfig{ Type: notionapi.PropertyConfigTypeDate, Date: struct{}{}, }, "Assignee": notionapi.PeoplePropertyConfig{ Type: notionapi.PropertyConfigTypePeople, People: struct{}{}, }, }, }) if err != nil { fmt.Printf("Error creating database: %v\n", err) return } fmt.Printf("Created database: %s\n", database.ID) fmt.Printf("URL: %s\n", database.URL) } ``` -------------------------------- ### List Workspace Users Source: https://context7.com/jomei/notionapi/llms.txt Retrieves a paginated list of all users within the workspace, including both individual people and bot accounts. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() response, err := client.User.List(ctx, ¬ionapi.Pagination{ PageSize: 100, }) if err != nil { fmt.Printf("Error listing users: %v\n", err) return } for _, user := range response.Results { fmt.Printf("User: %s (ID: %s, Type: %s)\n", user.Name, user.ID, user.Type) if user.Person != nil { fmt.Printf(" Email: %s\n", user.Person.Email) } if user.Bot != nil { fmt.Printf(" Workspace: %s\n", user.Bot.WorkspaceName) } } } ``` -------------------------------- ### Create a new Notion page using Go Source: https://context7.com/jomei/notionapi/llms.txt Creates a new page as a child of a database or another page. Requires a PageCreateRequest object containing properties that match the target database schema. ```go package main import ( "context" "fmt" "time" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() dueDate := notionapi.Date(time.Now().AddDate(0, 0, 7)) page, err := client.Page.Create(ctx, ¬ionapi.PageCreateRequest{ Parent: notionapi.Parent{ Type: notionapi.ParentTypeDatabaseID, DatabaseID: notionapi.DatabaseID("database_id"), }, Properties: notionapi.Properties{ "Name": notionapi.TitleProperty{ Type: notionapi.PropertyTypeTitle, Title: []notionapi.RichText{ {Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "New Task"}}, }, }, "Status": notionapi.SelectProperty{Type: notionapi.PropertyTypeSelect, Select: notionapi.Option{Name: "To Do"}}, }, }) if err != nil { fmt.Printf("Error creating page: %v\n", err) return } fmt.Printf("Created page: %s\n", page.ID) } ``` -------------------------------- ### Query Database Source: https://context7.com/jomei/notionapi/llms.txt Queries a database with filters, sorts, and pagination. Returns pages matching the specified criteria. Filters can be combined using AND/OR compound filters for complex queries. ```APIDOC ## POST /databases/{database_id}/query ### Description Queries a database with filters, sorts, and pagination. Returns pages matching the specified criteria. Filters can be combined using AND/OR compound filters for complex queries. ### Method POST ### Endpoint /databases/{database_id}/query ### Parameters #### Path Parameters - **database_id** (string) - Required - The ID of the database to query. #### Query Parameters None #### Request Body - **filter** (object) - Optional - Criteria to filter pages. Can be a PropertyFilter or a CompoundFilter (AndCompoundFilter, OrCompoundFilter). - **sorts** (array) - Optional - An array of SortObjects to sort the results. - **limit** (integer) - Optional - The maximum number of results to return per page. - **next_cursor** (string) - Optional - A cursor for fetching the next page of results. ### Request Example ```json { "filter": { "property": "Status", "select": { "equals": "In Progress" } }, "sorts": [ { "property": "Created", "direction": "descending" } ], "page_size": 50 } ``` ### Response #### Success Response (200) - **object** (string) - "list" - **results** (array) - An array of page objects matching the query. - **next_cursor** (string) - A cursor for fetching the next page of results, or null if there are no more pages. - **has_more** (boolean) - True if there are more pages of results, false otherwise. #### Response Example ```json { "object": "list", "results": [ { "object": "page", "id": "page_id_1", "properties": { ... } }, { "object": "page", "id": "page_id_2", "properties": { ... } } ], "next_cursor": "some_cursor_string", "has_more": true } ``` ``` -------------------------------- ### Search Pages and Databases with Notion API (Go) Source: https://context7.com/jomei/notionapi/llms.txt Searches all pages and databases shared with the integration using the Notion API. It supports filtering by object type (page or database) and sorting results by relevance or last edited time. Requires a Notion client initialized with an API token. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() // Search for pages and databases by title response, err := client.Search.Do(ctx, ¬ionapi.SearchRequest{ Query: "Project Plan", Sort: ¬ionapi.SortObject{ Timestamp: notionapi.TimestampLastEdited, Direction: notionapi.SortOrderDESC, }, PageSize: 20, }) if err != nil { fmt.Printf("Error searching: %v\n", err) return } for _, obj := range response.Results { switch result := obj.(type) { case *notionapi.Page: fmt.Printf("Page: %s (URL: %s)\n", result.ID, result.URL) case *notionapi.Database: fmt.Printf("Database: %s (URL: %s)\n", result.Title[0].PlainText, result.URL) } } // Search only for databases dbResponse, err := client.Search.Do(ctx, ¬ionapi.SearchRequest{ Query: "Tasks", Filter: notionapi.SearchFilter{ Value: "database", Property: "object", }, }) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Found %d databases\n", len(dbResponse.Results)) // Search only for pages pageResponse, err := client.Search.Do(ctx, ¬ionapi.SearchRequest{ Query: "Meeting Notes", Filter: notionapi.SearchFilter{ Value: "page", Property: "object", }, }) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Found %d pages\n", len(pageResponse.Results)) } ``` -------------------------------- ### Retrieve a Notion Page Source: https://github.com/jomei/notionapi/blob/main/README.md Use the initialized client to fetch details of a specific page by its ID. This operation requires a context and returns the page object or an error if the request fails. ```go page, err := client.Page.Get(context.Background(), "your_page_id") if err != nil { // Handle the error } ``` -------------------------------- ### Error Handling for Notion API in Go Source: https://context7.com/jomei/notionapi/llms.txt Illustrates how to handle various error scenarios when using the Notion API Go client, including specific typed errors for API issues and rate limiting. It shows how to check error types and extract relevant information like error codes and messages. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() page, err := client.Page.Get(ctx, notionapi.PageID("invalid_id")) if err != nil { // Check for Notion API error if apiErr, ok := err.(*notionapi.Error); ok { fmt.Printf("API Error Code: %s\n", apiErr.Code) fmt.Printf("Status: %d\n", apiErr.Status) fmt.Printf("Message: %s\n", apiErr.Message) return } // Check for rate limiting error (after max retries exhausted) if rateLimitErr, ok := err.(*notionapi.RateLimitedError); ok { fmt.Printf("Rate Limited: %s\n", rateLimitErr.Message) return } // Generic error fmt.Printf("Error: %v\n", err) return } fmt.Printf("Page: %s\n", page.URL) } ``` -------------------------------- ### Retrieve Current Bot User Source: https://context7.com/jomei/notionapi/llms.txt Retrieves profile information for the bot associated with the current integration token. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() me, err := client.User.Me(ctx) if err != nil { fmt.Printf("Error getting bot user: %v\n", err) return } fmt.Printf("Bot Name: %s\n", me.Name) fmt.Printf("Bot ID: %s\n", me.ID) if me.Bot != nil { fmt.Printf("Workspace: %s\n", me.Bot.WorkspaceName) } } ``` -------------------------------- ### Retrieve block children with pagination Source: https://context7.com/jomei/notionapi/llms.txt Retrieves nested content blocks for a given page or block ID. It includes logic for handling paginated results and recursive fetching of nested children. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() response, err := client.Block.GetChildren(ctx, notionapi.BlockID("page_or_block_id"), ¬ionapi.Pagination{ PageSize: 100, }) if err != nil { fmt.Printf("Error getting children: %v\n", err) return } for _, block := range response.Results { fmt.Printf("Type: %s, Content: %s\n", block.GetType(), block.GetRichTextString()) if block.GetHasChildren() { children, _ := client.Block.GetChildren(ctx, block.GetID(), nil) for _, child := range children.Results { fmt.Printf(" Child: %s\n", child.GetRichTextString()) } } } if response.HasMore { nextPage, _ := client.Block.GetChildren(ctx, notionapi.BlockID("page_or_block_id"), ¬ionapi.Pagination{ StartCursor: notionapi.Cursor(response.NextCursor), PageSize: 100, }) fmt.Printf("More blocks: %d\n", len(nextPage.Results)) } } ``` -------------------------------- ### Append Block Children using jomei/notionapi Source: https://context7.com/jomei/notionapi/llms.txt Demonstrates how to use the AppendChildren method to add multiple block types to a Notion page. It requires a valid Notion integration token and a target block or page ID, supporting complex block structures like rich text annotations and nested items. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() response, err := client.Block.AppendChildren(ctx, notionapi.BlockID("page_id"), ¬ionapi.AppendBlockChildrenRequest{ Children: []notionapi.Block{ ¬ionapi.Heading1Block{ BasicBlock: notionapi.BasicBlock{ Type: notionapi.BlockTypeHeading1, Object: notionapi.ObjectTypeBlock, }, Heading1: notionapi.Heading{ RichText: []notionapi.RichText{ {Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "Section Title"}}, }, }, }, ¬ionapi.ParagraphBlock{ BasicBlock: notionapi.BasicBlock{ Type: notionapi.BlockTypeParagraph, Object: notionapi.ObjectTypeBlock, }, Paragraph: notionapi.Paragraph{ RichText: []notionapi.RichText{ {Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "This is "}}, {Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "bold"}, Annotations: ¬ionapi.Annotations{Bold: true}}, {Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: " text."}}, }, }, }, ¬ionapi.BulletedListItemBlock{ BasicBlock: notionapi.BasicBlock{ Type: notionapi.BlockTypeBulletedListItem, Object: notionapi.ObjectTypeBlock, }, BulletedListItem: notionapi.ListItem{ RichText: []notionapi.RichText{ {Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "First item"}}, }, }, }, ¬ionapi.ToDoBlock{ BasicBlock: notionapi.BasicBlock{ Type: notionapi.BlockTypeToDo, Object: notionapi.ObjectTypeBlock, }, ToDo: notionapi.ToDo{ RichText: []notionapi.RichText{ {Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "Task to complete"}}, }, Checked: false, }, }, ¬ionapi.CodeBlock{ BasicBlock: notionapi.BasicBlock{ Type: notionapi.BlockTypeCode, Object: notionapi.ObjectTypeBlock, }, Code: notionapi.Code{ RichText: []notionapi.RichText{ {Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "fmt.Println(\"Hello, World!\")"}}, }, Language: "go", }, }, ¬ionapi.CalloutBlock{ BasicBlock: notionapi.BasicBlock{ Type: notionapi.BlockTypeCallout, Object: notionapi.ObjectTypeBlock, }, Callout: notionapi.Callout{ RichText: []notionapi.RichText{ {Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "Important note!"}}, }, Icon: ¬ionapi.Icon{ Type: "emoji", Emoji: (*notionapi.Emoji)(strPtr("💡")), }, }, }, }, }) if err != nil { fmt.Printf("Error appending blocks: %v\n", err) return } fmt.Printf("Appended %d blocks\n", len(response.Results)) } func strPtr(s string) *string { return &s } ``` -------------------------------- ### Retrieve Specific User Source: https://context7.com/jomei/notionapi/llms.txt Fetches details for a specific user based on their unique user ID. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() user, err := client.User.Get(ctx, notionapi.UserID("user_id")) if err != nil { fmt.Printf("Error getting user: %v\n", err) return } fmt.Printf("Name: %s\n", user.Name) fmt.Printf("Avatar: %s\n", user.AvatarURL) } ``` -------------------------------- ### Create Comment on Notion Page or Discussion (Go) Source: https://context7.com/jomei/notionapi/llms.txt Creates a comment on a Notion page or replies to an existing discussion thread using the Notion API. Comments can only be added to pages shared with the integration. Requires a Notion client and a page ID or discussion ID. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() // Create a new comment on a page comment, err := client.Comment.Create(ctx, ¬ionapi.CommentCreateRequest{ Parent: notionapi.Parent{ Type: notionapi.ParentTypePageID, PageID: notionapi.PageID("page_id"), }, RichText: []notionapi.RichText{ { Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "This is a comment on the page."}, }, }, }) if err != nil { fmt.Printf("Error creating comment: %v\n", err) return } fmt.Printf("Created comment: %s\n", comment.ID) fmt.Printf("Discussion ID: %s\n", comment.DiscussionID) // Reply to an existing discussion thread reply, err := client.Comment.Create(ctx, ¬ionapi.CommentCreateRequest{ DiscussionID: comment.DiscussionID, RichText: []notionapi.RichText{ { Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "This is a reply to the discussion."}, }, }, }) if err != nil { fmt.Printf("Error creating reply: %v\n", err) return } fmt.Printf("Created reply: %s\n", reply.ID) } ``` -------------------------------- ### Retrieve a single block by ID Source: https://context7.com/jomei/notionapi/llms.txt Fetches a specific block object from Notion using its unique identifier. The implementation demonstrates how to type-assert the block to access specific content like paragraph text or todo status. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() block, err := client.Block.Get(ctx, notionapi.BlockID("block_id")) if err != nil { fmt.Printf("Error getting block: %v\n", err) return } fmt.Printf("Block ID: %s\n", block.GetID()) fmt.Printf("Block Type: %s\n", block.GetType()) fmt.Printf("Has Children: %v\n", block.GetHasChildren()) switch b := block.(type) { case *notionapi.ParagraphBlock: fmt.Printf("Text: %s\n", b.GetRichTextString()) case *notionapi.Heading1Block: fmt.Printf("Heading 1: %s\n", b.GetRichTextString()) case *notionapi.ToDoBlock: fmt.Printf("Todo: %s (Checked: %v)\n", b.GetRichTextString(), b.ToDo.Checked) } } ``` -------------------------------- ### Update an existing Notion page using Go Source: https://context7.com/jomei/notionapi/llms.txt Updates specific properties, icons, covers, or the archive status of a page. Only the fields provided in the PageUpdateRequest are modified. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() page, err := client.Page.Update(ctx, notionapi.PageID("page_id"), ¬ionapi.PageUpdateRequest{ Properties: notionapi.Properties{ "Status": notionapi.SelectProperty{ Type: notionapi.PropertyTypeSelect, Select: notionapi.Option{Name: "Done"}, }, }, Archived: false, }) if err != nil { fmt.Printf("Error updating page: %v\n", err) return } fmt.Printf("Updated page: %s\n", page.ID) } ``` -------------------------------- ### Update Notion Database with Go Source: https://context7.com/jomei/notionapi/llms.txt Updates an existing Notion database's title or properties. This operation allows adding new properties or modifying existing ones without affecting other properties. It utilizes the notionapi Go client. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() database, err := client.Database.Update(ctx, notionapi.DatabaseID("database_id"), ¬ionapi.DatabaseUpdateRequest{ Title: []notionapi.RichText{ { Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "Updated Database Title"}, }, }, Properties: notionapi.PropertyConfigs{ "New Property": notionapi.RichTextPropertyConfig{ Type: notionapi.PropertyConfigTypeRichText, RichText: struct{}{}, }, }, }) if err != nil { fmt.Printf("Error updating database: %v\n", err) return } fmt.Printf("Updated database: %s\n", database.Title[0].PlainText) } ``` -------------------------------- ### Update Notion Block Content Source: https://context7.com/jomei/notionapi/llms.txt Updates the content and properties of an existing block by providing a BlockUpdateRequest. This operation replaces the entire value for the specified fields. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() block, err := client.Block.Update(ctx, notionapi.BlockID("block_id"), ¬ionapi.BlockUpdateRequest{ Paragraph: ¬ionapi.Paragraph{ RichText: []notionapi.RichText{ { Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "Updated paragraph content"}, }, }, Color: "blue_background", }, }) if err != nil { fmt.Printf("Error updating block: %v\n", err) return } fmt.Printf("Updated block: %s\n", block.GetID()) // Update a to-do item's checked status todoBlock, err := client.Block.Update(ctx, notionapi.BlockID("todo_block_id"), ¬ionapi.BlockUpdateRequest{ ToDo: ¬ionapi.ToDo{ Checked: true, RichText: []notionapi.RichText{ {Type: notionapi.RichTextTypeText, Text: ¬ionapi.Text{Content: "Completed task"}}, }, }, }) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Updated todo: %s\n", todoBlock.GetID()) } ``` -------------------------------- ### Delete Notion Block Source: https://context7.com/jomei/notionapi/llms.txt Archives a block by its ID. Deleted blocks are moved to the trash and can be restored via the Notion interface. ```go package main import ( "context" "fmt" "github.com/jomei/notionapi" ) func main() { client := notionapi.NewClient(notionapi.Token("secret_token")) ctx := context.Background() block, err := client.Block.Delete(ctx, notionapi.BlockID("block_id")) if err != nil { fmt.Printf("Error deleting block: %v\n", err) return } fmt.Printf("Deleted block: %s (archived: %v)\n", block.GetID(), block.GetArchived()) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.