Add dcoumentation for API (#18)

This commit is contained in:
KM Koushik
2024-05-22 19:43:17 +10:00
committed by GitHub
parent b9643f41e1
commit e0fc68d4c0
7 changed files with 100 additions and 14 deletions

View File

@@ -0,0 +1,24 @@
---
title: Introduction
description: "Fundamental concepts of Usend's API."
---
## Base URL
Unsend's API is built on REST principles and is served over HTTPS. To ensure data privacy, unencrypted HTTP is not supported.
The Base URL for all API endpoints is:
```sh Terminal
https://app.unsend.dev/api/
```
## Authentication
Authentication to Usend's API is performed via the Authorization header with a Bearer token. To authenticate, you need to include the Authorization header with the word Bearer followed by your token in your API requests like so:
```sh Terminal
Authorization: Bearer us_12345
```
You can create a new token/API key under your Unsend [Developer Settings](https://app.unsend.dev/api-keys).

View File

@@ -106,6 +106,18 @@
}, },
"/v1/emails/{emailId}": { "/v1/emails/{emailId}": {
"get": { "get": {
"parameters": [
{
"schema": {
"type": "string",
"minLength": 3,
"example": "1212121"
},
"required": true,
"name": "emailId",
"in": "path"
}
],
"responses": { "responses": {
"200": { "200": {
"description": "Retrieve the user", "description": "Retrieve the user",

View File

@@ -0,0 +1,35 @@
---
title: NodeJS
description: "Send your mail using unsend in NodeJS"
---
## Prerequisites
- [Unsend API key](https://app.unsend.dev/api-keys)
- [Verified domain](https://app.unsend.dev/domains)
## Code
```js server.ts
const options = {
method: "POST",
headers: {
Authorization: "Bearer us_12345",
"Content-Type": "application/json",
},
body: {
to: "jsmith@example.com",
from: "jsmith@example.com",
subject: "<string>",
replyTo: "<string>",
text: "<string>",
html: "<string>",
attachments: [{ filename: "<string>", content: "<string>" }],
},
};
fetch("https://app.unsend.dev/api/v1/emails", options)
.then((response) => response.json())
.then((response) => console.log(response))
.catch((err) => console.error(err));
```

View File

@@ -30,4 +30,11 @@ Quicklinks to set up your account and get started
> >
Learn how to use our API to send emails programmatically. Learn how to use our API to send emails programmatically.
</Card> </Card>
<Card
title="NodeJS"
icon="node-js"
href="/get-started/nodejs"
>
Learn how to use our API to send emails programmatically.
</Card>
</CardGroup> </CardGroup>

View File

@@ -47,11 +47,12 @@
{ {
"group": "Get Started", "group": "Get Started",
"pages": [ "pages": [
"introduction" "introduction",
"get-started/nodejs"
] ]
}, },
{ {
"group": "API Documentation", "group": "API Reference",
"pages": [ "pages": [
"api-reference/introduction" "api-reference/introduction"
] ]
@@ -76,7 +77,7 @@
"method": "bearer" "method": "bearer"
}, },
"playground": { "playground": {
"mode": "simple" "mode": "show"
} }
}, },
"footerSocials": { "footerSocials": {

View File

@@ -7,14 +7,10 @@ const ErrorCode = z.enum([
"BAD_REQUEST", "BAD_REQUEST",
"FORBIDDEN", "FORBIDDEN",
"INTERNAL_SERVER_ERROR", "INTERNAL_SERVER_ERROR",
"USAGE_EXCEEDED",
"DISABLED",
"NOT_FOUND", "NOT_FOUND",
"NOT_UNIQUE", "NOT_UNIQUE",
"RATE_LIMITED", "RATE_LIMITED",
"UNAUTHORIZED", "UNAUTHORIZED",
"PRECONDITION_FAILED",
"INSUFFICIENT_PERMISSIONS",
"METHOD_NOT_ALLOWED", "METHOD_NOT_ALLOWED",
]); ]);
@@ -25,9 +21,6 @@ function codeToStatus(code: z.infer<typeof ErrorCode>): StatusCode {
case "UNAUTHORIZED": case "UNAUTHORIZED":
return 401; return 401;
case "FORBIDDEN": case "FORBIDDEN":
case "DISABLED":
case "INSUFFICIENT_PERMISSIONS":
case "USAGE_EXCEEDED":
return 403; return 403;
case "NOT_FOUND": case "NOT_FOUND":
return 404; return 404;
@@ -35,8 +28,6 @@ function codeToStatus(code: z.infer<typeof ErrorCode>): StatusCode {
return 405; return 405;
case "NOT_UNIQUE": case "NOT_UNIQUE":
return 409; return 409;
case "PRECONDITION_FAILED":
return 412;
case "RATE_LIMITED": case "RATE_LIMITED":
return 429; return 429;
case "INTERNAL_SERVER_ERROR": case "INTERNAL_SERVER_ERROR":
@@ -52,12 +43,14 @@ function statusToCode(status: StatusCode): z.infer<typeof ErrorCode> {
return "UNAUTHORIZED"; return "UNAUTHORIZED";
case 403: case 403:
return "FORBIDDEN"; return "FORBIDDEN";
case 404: case 404:
return "NOT_FOUND"; return "NOT_FOUND";
case 405: case 405:
return "METHOD_NOT_ALLOWED"; return "METHOD_NOT_ALLOWED";
case 409:
return "NOT_UNIQUE";
case 429:
return "RATE_LIMITED";
case 500: case 500:
return "INTERNAL_SERVER_ERROR"; return "INTERNAL_SERVER_ERROR";
default: default:

View File

@@ -9,6 +9,20 @@ import { UnsendApiError } from "../../api-error";
const route = createRoute({ const route = createRoute({
method: "get", method: "get",
path: "/v1/emails/{emailId}", path: "/v1/emails/{emailId}",
request: {
params: z.object({
emailId: z
.string()
.min(3)
.openapi({
param: {
name: "emailId",
in: "path",
},
example: "cuiwqdj74rygf74",
}),
}),
},
responses: { responses: {
200: { 200: {
content: { content: {