Share to specific Notion page without Notion opening

I’m having the same problem exactly and just noticed this line in the script:

credential.authorize()

Followed by ‘var secret =…’ and ‘var inbox…’ commands.

I’m not a programmer, but I’m wondering if the line I referenced above is waiting for a variable of some sort.
I too have generated a secret from integrations and have add the page id.

Would love to get this to work.

What happens if you put that back? The first script step looks to be setting up credentials, but otherwise it just defines a set of functions. That second script step looks like it is calling the functions to do the work.

I wonder if the “Test Script” comment might refer to the commented out line that comes after, or if it is more an example script than a test script.

Note: I am not a Notion user, so I can’t check it actually running.

Good thinking there, but I just deleted the action and the credentials stored in preferences and readded the action and the page ID and secret and got the same behavior. The action runs successfully, but I don’t see any content on the page where I took the ID.

Hi @moggy1972, can you also add version to functions other than addDatabasePage()? such as addPage() and addTextBlock()? They are failing without the version line. Thanks!

Thanks for the feedback. I’ve added the version to the other functions, and tidied it up a bit and added more helpful comments.

1 Like

It works now. Thanks!

Thanks a lot @moggy1972 for this. It’s going to be an everyday run action from my drafts.
However, I could be perfect if I could add them to a database. Is it possible to implement that function?
Thanks!

Yes - it’s definitely possible. The add database call in the API is slightly different. The parent id goes in a field called “database_id”, rather than “page_id”. And the structure of the date is a little different.

For a non-database page, the body of the request just needs to be:
body = { “parent”: { “page_id”: YOUR_PAGE_ID },“properties”: {“title”: [{“text”: {“content”: YOUR_TITLE}}]}}

For a database page, it’s a little longer:
body = { “parent”: { “database_id”: YOUR_DATABASE_ID },“properties”: {“Name”: {“title”: [{“text”: {“content”: YOUR_TITLE}}]}}}

There has been a new release of the API with some additional functions so I’m planning on updating the actions over the next week or so, but there should be enough information here for you to be able to do it yourself for now.

1 Like

Thanks for the action.
I screwed up the initial setup but after reading the posts I got it to function.
But like other people the action seems happy but no data is transferred to Notion.
I have deleted the app and reinstalled but the initial setup questions never appear.
where can I delete it totally and start from scratch?
Appreciate your work, thanks
Pete

There’s two things I can think might be happening here.

  1. Your credentials are held within Drafts in Settings. I’m not sure if they persist after you delete Drafts, but you can tell Drafts to forget specific credentials through Settings/Credentials. If you select “forget” for the Notion credential, you should be prompted to add the secret next time you run the action.
  2. Within Notion, you need to set up an integration to use pages within databases or top-level pages. This is where you would get the secret key from. If you want to use a different area within your Notion workspace, you need to go to the database main page and use Share (top right) to share it with your integration.

Do either of those make sense/help?

Hi @moggy1972 - I have been having such a hair-brained time trying to settle on ONE Productivity/Canvas/Note ecosystem and your “Add to Notion” action might make that decision for me but I’m having trouble getting your “add to Notion” action to fire correctly.

I have recreated my Notion integration, deleted the credentials in Drafts and entered the Secret/Page ID in the dialog box but I immediately get the:

I saw where you suggest hard coding the information in the script but I was hoping you could get specific about what I should be replacing.

Is it one of these lines?

credential.addPasswordField(“secret”, “Secret”);
credential.addTextField(“inbox”, “Inbox”);

Edit: It only seems to error in a multi-line Draft. A single line pretends to work but doesn’t insert any data.

Edit Edit: Well…disregard I suppose. I reinstalled the action and it’s working for some reason - multiline and everything.

Now to figure out how to add to databases…

Thanks for the work @moggy1972 !

It’s on my todo list to tidy this all up - it’s not as clear as it should be! [I see it’s working for you now, but sharing in case this is helpful for others]

What I meant by hardcoding was that instead of using credentials, you could just have:

secret = "myintegrationsecret"
inbox = "pageid"

Where “secret” is the code that was created when you set up the integration with Notion. (find it in “Settings & Members/Integrations”) And “pageid” is the uuid of the page you want to add to.

It’s probably relatively low risk to hard code the “secret” - I just didn’t want it being shared when I shared my action.

The add to database action would be very similar - you just need to tweak the body of the javascript request to something like:

body = { "parent": { "database_id": databaseId },"properties": {"Name": {"title": [{"text": {"content": title}}]}}}

This only adds a new database page with a title - I have managed to add child blocks and properties as well, but can’t immediately find the code.

I should PROBABLY just be leaving well enough. I’ve killed the error I was getting about not being able to find the variable DATABASE_ID (capitalization) and adding to a page is still good but my test database isn’t being updated. I feel like there should be more in “Example 2” than what I have below:

// Example 2: Add item to database in Notion with the draft title as Action.
// Need to add the database ID

addDatabasePage(database_id, title)

body = { “parent”: { “database_id”:“44a53b6227184e2885a803e6927359d4” },“properties”: {“Name”: {“title”: [{“text”: {“content”: title}}]}}}

I think I’ve created enough functionality for my setup using the “Add to Notion” and a synced block on my Inbox page. So when a quick note from Drafts comes in I can drag it up to a record in my synced database.

I’ll keep an eye out for any updates you have time to do. Thank you so much for what you’ve done and being accessible for questions.

Can somebody please help me with this :weary:

I have tried

but I always get this error when I have multiple line draft and when I have single line draft it runs but nothing shows up in notion.

The magical part where it all of a sudden works is somehow not happening for me :disguised_face:

I don’t use Notion, but I have had a quick scan through the code. The line that is failing looks to be line 14 in the second script step - second line below.

page = addPage(inbox,title)
addTextBlock(page.id,note)

The fact that page.id is undefined will be down to the previous line.

The addPage function is defined in the previous script step and includes some logging for API errors.

What is shown in your action log? I’m expecting that there shod be a status code for the API call, followed by some “descriptive” error text.

Hi Sylumer

Thank you for your quick response :pray:

I tried pasting the page.id into the second script but that didn´t work. Is there a problem the code not being indented after I paste the page.id ? I tried pressing tab to make it indent but it didn´t work.


Here are the action logs from

  • Running the action when I have not pasted the page.id into the second script
  • Running the action after I have pasted the page.id into the second script


Still doesn´t work

I get the impression from the results that you are changing it about without understanding it and then just hoping it works.

I see one code in there from one run which is a page not found. So that’s potentially fundamental for an API call, assuming that’s what triggered it.

If you start again from the beginning with the original action and configure it as originally done, we can start from there and check if it works, config, error messages, etc. Otherwise, we’re dealing with a mystery target that you keep also moving!

Correct you are sir :disguised_face:

But after your first comment, the only changes I made was to try to paste the page.id into the second script.

But from the beginning

  • I reinstalled the action
  • Delted the credentials for notion (in drafts settings )

and when I run it again (for the first time) this is what I get after I have entered the secret key and the page.id after I run the action.

Screenshot 2022-01-05 at 16.08.54
Screenshot 2022-01-05 at 16.09.11

Briefly reviewing the script, the page.id error you are getting is consistent with what I would expect if the call the addPage in the previous line fails. The action itself wasn’t written with any error handling/report, you’d have to backtrack to add some debugging code in the addPage method to sort out what might be going wrong.

Note that the Notion API is in beta and has been changing a lot, so it would not surprise me if this example just needs some updates for changes made at their end. Using beta APIs is not for the squeamish.

I hope to add more direct support for Notion in Drafts once their API settles down and comes out of beta.

2 Likes

Okay, so the 404 error for page creation as being logged in the custom addPage() function would relate to the create a page API call. The API documentation for this lists the following for the 404 return code.

So, you need to look at these three points in terms of figuring out what is causing it.