Back

[SOLVED] Adding a document using Rest API

  • 0
  • Databases
  • Web
djcali
21 Nov, 2023, 17:55

Hello, I have created a sign up form in my app and currently using the Rest API server side to create a new user like:

TypeScript
            payload['email'] = email;
            payload['password'] = password;

            const response = await fetch(`${AppwriteEndpoint}/account/sessions/email`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-Appwrite-Project': AppwriteProject
                },
                body: JSON.stringify(payload)

            });```

I need to create a document in a collection called userData which will hold other information about the user,  I looked over the Rest API docs but i can not find how to create a document  or the correct endpoint. I am not even sure this is possible.

Thanks for any advice.
TL;DR
User was looking for help on how to add a document to a collection using the REST API. They initially found the solution in the documentation by changing the platform to REST. Another user pointed out that there are two separate POST requests, but the original user mentioned they had already figured it out. Solution: To add a document using the REST API, the original user used the following code: ``` const testPayload = { 'documentId': 'unique()', 'data': { 'firstName': 'Test', 'lastName': 'Person', 'handle': 'testperson', 'email': 'test@test
djcali
21 Nov, 2023, 18:45

I think i figured it out.

TypeScript
                'documentId': 'unique()',
                'data': {
                    'firstName': 'Test',
                    'lastName': 'Person',
                    'handle': 'testperson',
                    'email': 'test@test.com',
                }
            }

            const test = await fetch(`${AppwriteEndpoint}/databases/${Database.id}/collections/${Collection.userData}/documents`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-Appwrite-Project': AppwriteProject,
                    'X-Appwrite-Key': PRIVATE_APPWRITE_KEY,
                },
                body: JSON.stringify(testPayload)
            });```
fafa
21 Nov, 2023, 19:13

Hey, these are 2 completely seperate POST requests, are you still in need of help?

djcali
21 Nov, 2023, 19:15

No I solved the problem by trial and error. I wanted to know how i can add a document to a collection via REST API. I found how to do it in the docs by changing the platform to REST. Thanks anyway!

fafa
21 Nov, 2023, 19:24

Good to know! Please set your support title as [Solved], so people know it's done!

djcali
21 Nov, 2023, 20:18

[SOLVED] Adding a document using Rest API

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more