Jump to content

Notion API - Post to database

Hi all,

 

I recently started learning Flutter & the video I was watching used the Notion API to get records from the database.

 

However, I was wondering if anyone has added records to the database using the API? As I want to have my expenses app add records to the Notion Database.

 

I've tried reading the documentation but I found it hard to follow. If anyone has an example posting data to the Notion API, please do share.

 

Thank you in advance.

 

Link to comment
Share on other sites

Link to post
Share on other sites

You should be able to use the Notion Page API to create new records under whatever table you're using, so long as you have the unique ID of your table.

 

I haven't used Flutter extensively for entire projects yet, so I'm lacking in technical knowledge behind their modules, but following this Flutter documentation, performing an HTTP request looks rather simple.

 

Here's a short snippet as to what I would try, following both API documentation pages that I've linked above.

Do note that the dart http module needs to imported for the following example to work.

 

// All of this would be inside of an async function call, example given below.
// Should probably wrap this in a try..catch block as well.

Expense<List<Item>> addExpense() async {
  // Perform an HTTP POST request.
  final response = await http.post(
      // Your Notion database table url goes here.
      // The URL should be structured somewhat like this:
      // 'https://api.notion.com/v1/databases/YOUR_DATABASE_ID/query'
      Uri.parse('https://api.notion.com/v1/databases/YOUR_DATABASE_ID/query'),
      headers: <String, String> {
          HttpHeaders.authorizationHeader: 
              'Bearer <API_KEY>',
              'Notion-Version': <NOTION_VERSION>,
          'Content-Type': 'application/json; charset=UTF-8',
      },
      body: jsonEncode(<String, String> {
        'title': title,
      }),
    );

    if (response.statusCode == 201) {
      // If the server did return a 201 CREATED response,
      // then parse the JSON.
      return Expense.fromJson(jsonDecode(response.body));
    } else {
      // If the server did not return a 201 CREATED response,
      // then throw an exception.
      throw Exception('Failed to create new expense.');
    }
}

 

If someone with greater Flutter expertise could follow up with my given example, that'd be great.

Link to comment
Share on other sites

Link to post
Share on other sites

  • 4 weeks later...
On 9/28/2021 at 9:54 PM, Toxocious said:

You should be able to use the Notion Page API to create new records under whatever table you're using, so long as you have the unique ID of your table.

 

I haven't used Flutter extensively for entire projects yet, so I'm lacking in technical knowledge behind their modules, but following this Flutter documentation, performing an HTTP request looks rather simple.

 

Here's a short snippet as to what I would try, following both API documentation pages that I've linked above.

Do note that the dart http module needs to imported for the following example to work.

 


// All of this would be inside of an async function call, example given below.
// Should probably wrap this in a try..catch block as well.

Expense<List<Item>> addExpense() async {
  // Perform an HTTP POST request.
  final response = await http.post(
      // Your Notion database table url goes here.
      // The URL should be structured somewhat like this:
      // 'https://api.notion.com/v1/databases/YOUR_DATABASE_ID/query'
      Uri.parse('https://api.notion.com/v1/databases/YOUR_DATABASE_ID/query'),
      headers: <String, String> {
          HttpHeaders.authorizationHeader: 
              'Bearer <API_KEY>',
              'Notion-Version': <NOTION_VERSION>,
          'Content-Type': 'application/json; charset=UTF-8',
      },
      body: jsonEncode(<String, String> {
        'title': title,
      }),
    );

    if (response.statusCode == 201) {
      // If the server did return a 201 CREATED response,
      // then parse the JSON.
      return Expense.fromJson(jsonDecode(response.body));
    } else {
      // If the server did not return a 201 CREATED response,
      // then throw an exception.
      throw Exception('Failed to create new expense.');
    }
}

 

If someone with greater Flutter expertise could follow up with my given example, that'd be great.

Sorry for the late reply mate. I decided to host my own web server and custom api with an sql database instead haha Thank you thought!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×